我有一个MyLinearLayout子类扩展了ViewGroup,这使得其中的View自动包装。

public class MyLinearLayout extends ViewGroup {
    private final int cellHeight = 70;


    public MyLinearLayout (Context context, AttributeSet attrs) {
        super (context, attrs);
    }


    @Override
    protected void onLayout (boolean changed, int l, int t, int r, int b) {
        int left = 40;
        int top = 20;
        int count = getChildCount ();
        int remainingWidth = 0;

        for (int j = 0; j < count; j++) {
            View childView = getChildAt (j);

            int w = childView.getMeasuredWidth ();
            int h = childView.getMeasuredHeight ();

            if (left != 40 && remainingWidth < w)
            {
                left = 40;
                top += (cellHeight + 20);
            }
            childView.layout (left, top, left + w, top + h);
            remainingWidth = r - l - left - w - 80;
            left += (w + 40);
        }
    }


    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        int cellWidthSpec = MeasureSpec.makeMeasureSpec (0,
                MeasureSpec.UNSPECIFIED);
        int cellHeightSpec = MeasureSpec.makeMeasureSpec (cellHeight,
                MeasureSpec.EXACTLY);
        int count = getChildCount ();

        for (int i = 0; i < count; i++) {
            View childView = getChildAt (i);
            childView.measure (cellWidthSpec, cellHeightSpec);
        }

        setMeasuredDimension (resolveSize (0, widthMeasureSpec),
                resolveSize (cellHeight * count, heightMeasureSpec));
    }


    public void removeChildViews (ViewGroup rootView) { // not work
        View child;
        if (rootView == null) {
            rootView = this;
        }
        int count = rootView.getChildCount ();
        Log.i ("--->", count + " start");
        for(int i = 0; i < count; i++) {
            child = rootView.getChildAt(i);
            Log.i ("--->", rootView.getClass ().toString () + " ROOT");
            Log.i ("--->", child.getClass ().toString () + " CHILD");
            if(child instanceof ViewGroup) {
                removeChildViews ((ViewGroup) child);
            } else if (child != null) {
                rootView.removeView (child);
            }
        }
        Log.i ("--->", rootView.getChildCount () + " end");
    }

}


然后在布局文件中声明它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/input_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    ...

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
        ...

        <com.test.MyLinearLayout
            android:id="@+id/llayout_autobreak"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </com.test.MyLinearLayout>
    </LinearLayout>
    ...
</LinearLayout>


并在我的Activity中使用它:

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.layout_input);

        ...

        ll_ab = (MyLinearLayout) findViewById (R.id.llayout_autobreak);

        spinner.setOnItemSelectedListener (new OnItemSelectedListener () {

                    @Override
                    public void onItemSelected (AdapterView <?> parent,
                            View view, int position, long id) {

                        ll_ab.removeChildViews(null); // not work
                        ...
                        ...
                    }

                    @Override
                    public void onNothingSelected (AdapterView <?> parent) {

                    }
                });

                ...
    }


我的ll_ab中的Activity布局可能包含许多不同的View,例如TextViewEditViewSpinner等...,我可以将它们动态添加到ll_ab布局中,并且它运作良好。但是,但是当我尝试用一​​组新的View替换它们时,我的removeChildViews (ViewGroup)中的MyLinearLayout似乎只能删除TextView,而不能删除其他。我也尝试使用removeAllViews(),但是它也不起作用。那么,如何删除ll_ab中的所有视图?十分感谢你的帮助。

最佳答案

您将null传递给removeChildViews,并且由于null没有子级,因此不会发生任何事情。您需要传递正确的父项才能removeChildViews。

09-10 06:02