如何指定ID时的用途包括在布局XML文件 [英] How to specify id when uses include in layout xml file

查看:118
本文介绍了如何指定ID时的用途包括在布局XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的布局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.

这篇关于如何指定ID时的用途包括在布局XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆