Android 使用包含在布局xml文件中时如何指定id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1759099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify id when uses include in layout xml file
提问by hap497
In my layout xml file, I have included other layout xml file (each with a different android id).
在我的布局 xml 文件中,我包含了其他布局 xml 文件(每个文件都有不同的 android id)。
<include layout="@layout/view_contact_name" android:id="+id/test1"/>
<include layout="@layout/view_contact_name" android:id="+id/test2"/>
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.
但是当我在模拟器中运行它并启动 Hierarchy Viewer 时,每个布局仍然显示“NO_ID”,并且在我的代码中,我有
findViewById(R.id.test1)
并且findViewById(R.id.test2)
都返回 null。
Can anyone please help me with my problem ?
任何人都可以帮我解决我的问题吗?
回答by Ron Romero
Specify the ID in the <include>
在 ID 中指定 <include>
<include layout="@layout/test" android:id="@+id/test1" />
Then use two findViewById
to access fields in the layout
然后使用两个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.
使用这种方法,您可以访问您拥有的任何包含中的任何字段。
回答by loki19
I found out, that if you are using <merge>
tag in your include layout, then the ID of include transfers to the merge tag which is not real view.
我发现,如果您<merge>
在包含布局中使用标签,则包含的 ID 会转移到非真实视图的合并标签。
So either remove merge, or replace it with some layout.
所以要么删除合并,要么用一些布局替换它。
Tor Norbye wrote:
Tor Norbye写道:
The
<include>
tag is not a real view, so findByView will not find it. The @id attribute (and any other attributes you've set on the include tag) gets applied on the root tag of the included layout instead. So your activity.getView(R.id.included1) should in fact be the<TextView>
itself.
该
<include>
标签是不是一个真正的观点,所以findByView不会找到它。@id 属性(以及您在 include 标记上设置的任何其他属性)改为应用于所包含布局的根标记。所以你的 activity.getView(R.id.included1) 实际上应该是<TextView>
它本身。
回答by Daniel Yankowsky
回答by Andre Romano
I think the top answer misses the most important point and might mislead people into thinking the <include/>
tag creates a View that holds the include contents.
我认为最重要的答案忽略了最重要的一点,可能会误导人们认为<include/>
标签创建了一个包含包含内容的视图。
The key point is that include's idis passedto the root view of the include's layout file.
关键是包含的id被传递到包含的布局文件的根视图。
Meaning that this:
这意味着:
// activity_main.xml
<include layout="@layout/somelayout" android:id="@+id/someid"/>
// somelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Becomes this:
变成这样:
// activity_main.xml
<ImageView
android:id="@+id/someid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
回答by jokernk
yes is like this, but careful when the layout inserted in include field is a custom one and you want to access that root layout. That layout in this case @layout/test test, is actually returned in first line.
是的就是这样,但是当插入包含字段的布局是自定义布局并且您想要访问该根布局时要小心。在这种情况下@layout/test test 的布局实际上是在第一行返回的。
test test1View = (test)findViewById(R.id.test1);
回答by Sinan Ergin
- you must set id each include tag
- included child element set a new id. if you look how to generate new id, look at this entry: https://stackoverflow.com/a/15442898/1136117
- 您必须为每个包含标签设置 id
- 包含的子元素设置了一个新的 id。如果您查看如何生成新 ID,请查看此条目:https: //stackoverflow.com/a/15442898/1136117
回答by Prakash
Problem is we try to use id which is not declared in current layout file.
Instead of declaring again, id can be simply referred using @+id/
. If you refactor original id name through Android Studio it does refactor in included layout as well.
问题是我们尝试使用未在当前布局文件中声明的 id。而不是再次声明, id 可以简单地使用@+id/
. 如果您通过 Android Studio 重构原始 id 名称,它也会重构包含的布局。
<include layout="@layout/toolbar"/>
<TextView
android:id="@+id/txt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
**android:layout_below="@+id/toolbar"**
android:layout_marginTop="16dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"/>
回答by Shanki Bansal
In a case of using <RecyclerView>
find the id of <include>
by using an instance of inflated view or else it will return null.
在使用膨胀视图的实例<RecyclerView>
查找 id<include>
的情况下,否则它将返回null。
public class ViewHolder extends RecyclerView.ViewHolder {
private mTextView;
public ViewHolder(View view) {
super(view);
View include_1 = view.findViewById(R.id.include_1);
mTextView = (TextView) include_1.findViewById(R.id.text_id);
}
}
回答by Upendra Shah
If you have set id to either root tag of included layout then you can use that id or you can set id to included layout.
如果您已将 id 设置为包含布局的根标记,那么您可以使用该 id 或者您可以将 id 设置为包含布局。
But you can not set id to both it may throw exception.
但是您不能将 id 设置为两者可能会引发异常。
<include layout="@layout/view_contact_name" android:id="+id/test1"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</LinearLayout>
Or
或者
<include layout="@layout/view_contact_name"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/llBottomMainView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</LinearLayout>
回答by TheHebrewHammer
Wow, I can't believe this question doesn't have the right answer yet. It's simple tags suck. You can only change things that start with android:layout_
which android:id
doesn't match. So the answer is you can't. Sorry. What you can do instead is create a class that will be a ViewGroup which will inflate the included views inside, then add that as a tag in your layout, but that's about it.
哇,我不敢相信这个问题还没有正确答案。很简单的标签很烂。您只可以改变的事情,与启动android:layout_
其android:id
不匹配。所以答案是你不能。对不起。相反,您可以做的是创建一个类,该类将是一个 ViewGroup,它将在内部膨胀包含的视图,然后将其作为标签添加到您的布局中,仅此而已。