使用包含在布局xml文件中时如何指定id [英] How to specify id when uses include in layout xml file

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

问题描述

在我的布局 xml 文件中,我包含了其他布局 xml 文件(每个使用不同的 android 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"/>

但是当我在模拟器中运行它并启动 Hierarchy Viewer 时,每个布局仍然显示NO_ID",在我的代码中,我有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 ?

推荐答案

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

然后使用两个findViewById来访问布局中的字段

Then use two findViewById to access fields in the layout

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.

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

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