<分区>
您好,我想知道有什么方法可以检查父布局的子布局吗?
有没有id?
<分区>
您好,我想知道有什么方法可以检查父布局的子布局吗?
有没有id?
最佳答案
如果你有一个父 View 的子实例,你想在其中迭代,你可以遵循这个实现。
您可以按View
类型、id 或标签进行搜索。
//layout being the child of the view you have
View parent = layout.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View v = parent.getChildAt(i);
//view type
if (v instanceof ImageView) {
//whatever
} else if (v instanceof TextView) {
//whatever
} //
//by tag
if (v.getTag() instanceof yourObject) {//or == yourObject
//whatever
} else if (v.getTag() instanceof yourOtherObject) {//or == yourOtherObject
//whatever
}
//by id
if (v.getId().equals(searchingForView.getId())) {
//whatever
} else if (v.getId().equals(searchingForOtherView.getId())) {
//whatever
}
}
但是如果你知道id,你显然可以做到
View parent = layout.getParent();
View lookingForThisView = parent.findViewById(R.id.lookingforthisviewid);
关于android - 检查布局是否包含任何 ImageView 或 TextView android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637483/