用户登录
用户注册

分享至

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

  • 作者: 壮哉丶
  • 来源: 51数据库
  • 2021-08-07


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


2020-08-22

在使用 include 标签时产生了关于 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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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="http://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> 

本文地址:http://www.51sjk.com/Upload/Articles/1/0/253/253277_20210627002147267.jpg

软件
前端设计
程序设计
Java相关