如何指定ID时的用途包括在布局XML文件布局、用途、文件、ID

2023-09-05 06:08:15 作者:﹋服务YUAN ヽ来份勇气

在我的布局xml文件,我已经包括了其他的布局xml文件(每个 用不同的机器人id)的

In my layout xml file, I have included other layout xml file (each with a different android id).

<include layout="@layout/view_contact_name" android:id="+id/test1"/>
<include layout="@layout/view_contact_name" android:id="+id/test2"/>

但是,当我运行它在模拟器,并启动层次浏览器,每个 布局仍显示NO_ID',在我的code,我有 findViewById(R.id.test1) findViewById(R.id.test2)这两个返回null。

But when I run it in the emulator, and start Hierarchy Viewer, each of the layout still shows 'NO_ID', and in my code, I have findViewById(R.id.test1) and findViewById(R.id.test2) both returns null.

任何人都可以请帮我与我的问题?

Can anyone please help me with my problem ?

推荐答案

在指定ID的&LT;包括&GT;

<include layout="@layout/test" android:id="@+id/test1" />

然后使用两个 findViewById 来访问领域的布局

View test1View = findViewById(R.id.test1);
TextView test1TextView = (TextView) test1View.findViewById(R.id.text);

使用这种方法,您可以访问任何领域的任何有你有。

Using that approach, you can access any field in any include you have.