android开发分享[Android] include 的 id 和子布局的 id 之间的关系和作用(未解决)

2020-08-22问题Example 1Example 2Example 3Example 4Example 5Example 6Example 72020-08-22在使用 include 标签时产生了关于 id 的疑问,网上搜索了很多信息,杂乱无章,心烦意乱,未能找到答案,发表此文,记录一下疑问。如果有大神能以评论、私信等方式帮我解惑,我将不胜感激。如果没有,那将来我知道了答案必来更新本文。(没更新就是没答案,不必评论追问)问题当使用 findViewById 以下代码中出现的 id,获取.

  • 2020-08-22
    • 问题
    • Example 1
    • Example 2
    • Example 3
    • Example 4
    • Example 5
    • Example 6
    • Example 7

2020-08-22

在使用 include 标签时产生了关于 id 的疑问,网上搜索了很多信息,杂乱无章,心烦意乱,未能找到答案,发表此文,记录一下疑问。如果有大神能以评论、私信等方式帮我解惑,我将不胜感激。如果没有,那将来我知道了答案必来更新android开发分享[Android] include 的 id 和子布局的 id 之间的关系和作用(未解决)。(没更新就是没答案,不必评论追问)

问题

  1. 当使用 findViewById 以下代码中出现的 id,获取到的是什么?
  2. 这些 id 的作用是什么?
  3. 怎么获取到 TextView 这个控件?(为了节省空间,TextView 的 id 没有写,假设每个 TextView 都有 android:id=”@+id/txt”)

当然了,下面这个 Example 1 里是没有 id 的

Example 1

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </RelativeLayout> 

Example 2

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/include_tag" layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </RelativeLayout> 

Example 3

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/child" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </RelativeLayout> 

Example 4

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include id="@+id/include_tag" layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/child" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </RelativeLayout> 

Example 5

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/include_tag" layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </merge> 

Example 6

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/child" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </merge> 

Example 7

setContentView 调用的布局文件:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include id="@+id/include_tag" layout="@layout/child"/> </RelativeLayout> 

child.xml

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/child" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="123"/> </merge> 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/893652.html

(0)
上一篇 2021年10月20日
下一篇 2021年10月20日

精彩推荐