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

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>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值