gpt4 book ai didi

java - 创建 subview 时能否通知父 View

转载 作者:行者123 更新时间:2023-12-01 19:11:47 25 4
gpt4 key购买 nike

在 android UI 中可以动态创建,所以有没有办法知道子级是否是为父级创建的?

例如,如果我有一个线性布局作为父按钮,并且我动态创建一个子按钮。有没有通知家长?

最佳答案

Tal Kanel 的版本可以工作,但为了避免重复代码,我建议使用 HierarchyChangeListener:

LinearLayout ll = (LinearLayout)findViewById(R.id.mylinearlayout);
ll.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
public void onChildViewAdded(View parent, View child) {
//handle the logic for an added child here
}
public void onChildViewRemoved(View parent, View child) {
//optionally, handle logic for a removed child
}
});

关于java - 创建 subview 时能否通知父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8169526/

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