我有一个MyLinearLayout
子类extends 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
,例如TextView
,EditView
,Spinner
等。 ..,我可以动态地将它们添加到ll_ab
布局,并且效果很好。但是,当我尝试使用一组新的View
替换它们时,removeChildViews (ViewGroup)
中的MyLinearLayout
似乎只会删除TextView
而不会删除其他removeAllViews()
。我也尝试使用ll_ab
,但它也不起作用。那么,如何删除{{1}}中的所有视图?非常感谢你的帮助。
答案 0 :(得分:0)
您将null传递给removeChildViews,并且由于null没有子节点,因此不会发生任何事情。您需要将正确的父级传递给removeChildViews。