gpt4 book ai didi

java - removeView( View )在 ViewGroup 的子类中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:09 24 4
gpt4 key购买 nike

我有一个 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) {

}
});

...
}

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

最佳答案

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

关于java - removeView( View )在 ViewGroup 的子类中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28426104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com