日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2023/12/15 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发面试题之遍历ViewGroup拿到所有的ViewGroup和View的id 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

面試題如題:

咱們老套路先上圖:下面是我通過遍歷拿到的所有的id,怎么做的呢?

咱們先說下思路:

首先拿到最外層的ViewGroup然后通過它拿到它所有的child然后循環(huán)每個child判斷是ViewGroup還是View,如果是ViewGroup就繼續(xù)遍歷(遞歸),不是VieGroup的話那就是View了,那就直接打印View的id即可。

看代碼吧。

//遍歷樹形結(jié)構(gòu)viewViewGroup 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"><EditTextandroid:id="@+id/et_input"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:inputType="text"android:text="408" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="showResult"android:text="通過MVP模式顯示結(jié)果" /><LinearLayoutandroid:id="@+id/ll_two"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:id="@+id/tw_three"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tv_one"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tv_two"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><TextViewandroid:id="@+id/tv_three"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><ScrollViewandroid:id="@+id/sv_four"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv_shoe_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="@android:color/black" /></ScrollView></LinearLayout>

懂了吧。

總結(jié)

以上是生活随笔為你收集整理的Android开发面试题之遍历ViewGroup拿到所有的ViewGroup和View的id的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。