Android开发面试题之遍历ViewGroup拿到所有的ViewGroup和View的id

面试题如题:

咱们老套路先上图:下面是我通过遍历拿到的所有的id,怎么做的呢?

咱们先说下思路:

首先拿到最外层的ViewGroup然后通过它拿到它所有的child然后循环每个child判断是ViewGroup还是View,如果是ViewGroup就继续遍历(递归),不是VieGroup的话那就是View了,那就直接打印View的id即可。

看代码吧。

 //遍历树形结构view
        ViewGroup llRoot = findViewById(R.id.ll_root);
        forData(llRoot);
   

/**
     * 遍历ViewGroup的方法
     *
     * @param llRoot 根VieGroup
     */
    private void forData(ViewGroup llRoot) {
        int childCount = llRoot.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (llRoot.getChildAt(i) instanceof ViewGroup) {
                Log.e("打印ViewGroup的id", llRoot.getChildAt(i).getId() + "=");
                forData((ViewGroup) llRoot.getChildAt(i));
            } else {
                Log.e("打印View的id", llRoot.getChildAt(i).getId() + "=");
            }
        }
    }

咱们再看下xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="text"
        android:text="408" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showResult"
        android:text="通过MVP模式显示结果" />

    <LinearLayout
        android:id="@+id/ll_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/tw_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/sv_four"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tv_shoe_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />

    </ScrollView>

</LinearLayout>

懂了吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值