android - 检查布局是否包含任何 ImageView 或 TextView android

标签 android android-layout

<分区>

您好,我想知道有什么方法可以检查父布局的子布局吗?

有没有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/

相关文章:

android - 应用程序中的 Visual Studio Android 模拟器定期黑屏

android - 在 Android 的 Multichoice AlertDialog 中设置复选框状态

android - 文本与另一个文本的右下对齐

android - 如何在android上显示从文本编辑器(如tinymce)插入的数据?

android - Google Plus 磁贴动画

Android Layout 不会横向垂直滚动

android - AutoCompleteTextView:从先前提出的建议列表中过滤结果

android - 线性布局权重

android - 将原生 Twilio Android SDK 与 Flutter 集成

android - 图像在 viewflipper 中居中