在 XML 中的 "include" 布局中引用视图的 ID

7
我有一个按钮A,在special_button.xml中,我在所有的Activity中重复使用它。每个Activity都有一个根RelativeLayout
问题是:我有一个Activity中有一个按钮BA在同一位置,我想将B移到A上面,但我无法从Activity的xml文件中引用A
以下是xml文件: 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>
2个回答

4
当您使用include标记时,您还应该在XML中指定一个新的id,然后可以将其作为RelativeLayout中的id引用。请参阅文档和本示例代码:

您也可以通过在标记中指定它们来覆盖包含布局的根视图的所有布局参数(任何android:layout_*属性)。例如:

<include android:id="@+id/news_title"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     layout="@layout/title"/>

编辑:正如 @eskimoapps.com 指出的,使用 RelativeLayout 进行此操作似乎存在已知问题,如此处所述 here,但是当我运行以下代码时,它按照 OP 请求的方式工作:

<?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/Aid/B下面。

enter image description here

我唯一的想法是,Eclipse不喜欢它,因为它在尝试在同一布局文件中静态查找按钮。由于<include><merge>与LayoutManager动态工作,这可能是为什么这仍然按预期工作的原因。

请尝试并查看是否对您也起作用。


我已经尝试过这个方法。这种方法的问题在于id被赋予了包含布局的根元素。在我的情况下,它是merge标签,实际上是活动的RelativeLayout。我仍然无法访问按钮A。 - aysonje
您的陈述是不正确的,如果您给<include>一个与RelativeLayout的ID不同的ID。请检查我的修改并查看是否有帮助。 - anddev84
哇,谢谢!我不知道你可以直接引用A。只是想让你知道,我没有向<include>标签添加任何数据,只是简单地写了<include layout="@layout/special_button" />,它就起作用了。 - aysonje
非常感谢!我不知道include标签的"id"(和其他属性)会覆盖包含布局中根视图的属性。你的第一段话让我茅塞顿开 :) - Vinay Vissh

0
你可以像这样在你的<include>中添加一个id属性:
<include android:id="@+id/someId"
         layout="@layout/special_button"/>

我已经尝试过这个方法。这种方法的问题在于id被赋予了包含布局的根元素。在我的情况下,它是merge标签,实际上是活动的RelativeLayout。我仍然无法访问按钮A。 - aysonje
使用这种方式,您将无法直接从 XML 引用按钮 A,但现在您可以引用 <include> 中定义的新 ID。这将在您提到的 layout_above 值中起作用。 - anddev84
如果使用合并标记(id、visibility 和 layout_* 标记被忽略),则此方法实际上不起作用,而且如果未指定 layout_width 和 layout_height,则也不起作用,请参见 https://dev59.com/G3E95IYBdhLWcg3watU3。 - eski
通过使用附加到include的id,我将引用覆盖整个屏幕的RelativeLayout。我仍然不知道如何将按钮B放在按钮A上方。 - aysonje
@eskimoapps.com:感谢提供链接。虽然Macarse的方法更加整洁,但我认为它无法解决我的问题。我将尝试使用alienjazzcat的方法。 - aysonje

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接