在"include"窗口中引用视图的ID.xml中的布局 [英] Reference the id of a view inside an "include" layout in xml
问题描述
我在 special_button.xml
内有一个按钮 A
,可以在所有活动中重复使用.每个活动都有一个根 RelativeLayout
.
I have a button, A
, inside special_button.xml
, that I reuse on all my activities. Each activity has a root RelativeLayout
.
问题:我的一项活动在与 A
相同的位置上有一个按钮 B
.我决定只是将 B
移到 A
上方,但无法从活动的xml引用 A
.
The problem: one of my activities has a button, B
, on the same position as A
. I decided to just move B
above A
, but I can't reference A
from the activity's xml.
这是xmls
special_button.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="@+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"/>
</merge>
layout_main.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" >
<include layout="@layout/special_button" />
<Button
android:id="@+id/B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above="<id to reference button A>"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp" />
</RelativeLayout>
推荐答案
使用 include
标记时,还应在该XML中指定一个新ID,然后可以将其用作ID在 RelativeLayout
中.请参见文档和以下示例代码:
When you use the include
tag, you should also specify a new id within that XML, which you can then reference as the id within the RelativeLayout
. See the documentation and this sample code:
您还可以通过在标记中指定布局参数的根视图来覆盖所有布局参数(任何android:layout_ *属性).例如:
You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag. For example:
<include android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/title"/>
正如@ eskimoapps.com所指出的那样,使用 RelativeLayout
s似乎存在一个已知问题,如
As @eskimoapps.com points out, there appears to be a known issue doing this with RelativeLayout
s, as documented here, but when I run the following code, it is working as OP requests:
<?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/special_button"
android:id="@+id/btn_a_ref"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above="@+id/A" <!-- this still works, despite having warning in Eclipse -->
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp" />
</RelativeLayout>
这是HierarchyViewer中显示正确布局的图片: id/B
Here is the picture from HierarchyViewer showing the correct layout: id/A
below id/B
我唯一的想法是Eclipse不喜欢它,因为它是在静态尝试在同一布局文件中查找Button.由于< include>
和< merge>
与LayoutManager动态配合使用,因此可能仍然可以按预期运行.
My only thought is that Eclipse doesn't like it since it's statically trying to find the Button within the same layout file. Since <include>
and <merge>
work dynamically with the LayoutManager, that's probably while this still works as expected.
请尝试看看它是否也适合您.
Please try and see if this works for you as well.
这篇关于在"include"窗口中引用视图的ID.xml中的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!