3

我的布局是这样的

<ParentLinearlayout>
    <ChildLinearlayout>
       <Button>
    </ChildLinearlayout>
    <ChildLinearlayout>
       <Button>
    </ChildLinearlayout>
</ParentLinearlayout1>

ChildLinearlayout的数量被改变,动态添加和删除。

单击按钮时,我想删除作为 Button 父级的 ChildLinearlayout。

但我不知道索引线性布局。索引经常改变,因此不能使用标签。

如何知道 LinearLayout 索引?

4

4 回答 4

5
int index = linearLayout.indexOfChild(view);
linearLayout.removeViewAt(index);
于 2015-06-02T19:56:42.243 回答
1

另一种更强大的方法是在将每个子项添加到父项时为其分配一个标签。

View child = // TODO
child.setTag("child" + i);
parent.addView(child);

并使用 findViewByTag() 再次定位它。

parent.removeView(parent.findViewByTag("child4"));
于 2013-03-25T01:43:14.730 回答
0
parent.removeView((View)button.getParent());

不过要小心这一点,因为它非常依赖于您的视图层次结构。

于 2013-03-25T01:40:12.297 回答
0
public void showListofData() {
    if (linearlayout.getChildCount() > 0) {
        linearlayout.removeAllViews();
    }
    for (int i = 0; i < mylist.size(); i++) {
        LayoutInflater inflater = null;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View mLinearView = inflater.inflate(
                R.layout.row, null);

        TextView cName = (TextView) mLinearView
                .findViewById(R.id.txt_name);
        clinicName.setText(mylist.get(i).name);
        mLinearView.setTag(mylist.get(i));
        linearlayout.addView(mLinearView);

        mLinearView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
             int pos=(Integer)v.getTag();

            }
        });
    }
}
于 2015-06-30T11:05:34.983 回答