0

我有一个以编程方式膨胀的父自定义布局(LinearLayout 的子级)。

在构造函数中,我添加另一个布局作为父布局的子布局,如下所示:

public ExtendedContentLinearLayout(Context context) {
    super(context);
    this.context = context;

    this.setClickable(false);

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mTrigger = inflater.inflate(R.layout.nav_menu_board_expandable_children_outer, this, true);
    mTrigger.setOnClickListener(this);
    mTrigger.setBackgroundResource(R.color.da_blue);


}

父布局和“触发器”布局(上面刚刚添加的孩子)工作并显示正常。

一旦用户触摸了这个“触发”布局,我想向父级添加一个新子级 - 另一个包含更多内容的布局,因此我需要父级布局来扩展并包含新添加的布局,但这不起作用!

单击触发器布局不会添加新布局(至少未在屏幕上显示)。这是点击调用(点击事件正在工作):

   @Override
    public void onClick(View v) {
        App.disableTouchEventForDefaultDuration();

    Log.d("CLICKED", "CLICKED");


    LinearLayout mContent = new LinearLayout(context);
    mContent.setOrientation(HORIZONTAL);
    mContent.setBackgroundResource(R.color.da_indicator_pink);
    mContent.setVisibility(VISIBLE);
    ExtendedContentLinearLayout.this.addView(mContent);


}
4

3 回答 3

1

我最终在 XML 中创建了布局,然后使用:

 View view = inflater.inflate(this.layout, null, false);

然后使用 addView(view) 添加这个视图

这对我来说很好

于 2015-02-04T08:21:22.373 回答
0
mContent.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

请尝试上面的代码,尝试 WRAP_CONTENT 和 MATCH_PARENT

于 2015-02-02T10:41:00.477 回答
0

就我而言,layout.post有效

ExtendedContentLinearLayout.this.post( new Runnable() {
    @Override
    public void run() {
        ExtendedContentLinearLayout.this.addView(llhItem);
    }
});
于 2021-08-22T04:33:54.397 回答