gpt4 book ai didi

android - removeAllViews() 对 subview 有什么作用?

转载 作者:行者123 更新时间:2023-11-29 15:12:14 26 4
gpt4 key购买 nike

我的布局中有以下层次结构:

ScrollView
RadioGroup
RelativeLayout
RadioButton
ImageView
RelativeLayout
RadioButton
ImageView
...

现在的重点是,它在 XML 编辑器中看起来不错,其中 RadioButtonsImageViews 定义了默认值(占位符),但是当我启动一个 Activity 并调用RadioGroup 上的removeAllViews(),所有ImageViews 都消失了。有趣的是,所有按钮都获得了新值,只有 ImageViews 没有更新(设置新的源图像没有结果)。

所以,我的问题是:removeAllViews() 是完全删除 subview (就像它们从未存在于布局 XML 文件中一样)还是只是删除一些值,留下要定义的 View 参数(比如设置新的源图像或新的按钮描述)?

最佳答案

来自official documentation , removeAllViews():

Call this method to remove all child views from the ViewGroup.

调用此方法会将所有 subview 设置为null,因此它将从自身中删除 subview ,并且此 subview 变为无效(或不被视为子),但不像它在 XML 文件中从未存在过。

这是 removeAllViews() code :

public void removeAllViews() {
removeAllViewsInLayout();
requestLayout();
invalidate();
}

正如您在 removeAllViewsInLayout() 中看到的那样方法,它将子值设置为空:

children[i] = null;

关于android - removeAllViews() 对 subview 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926008/

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