【问题标题】:Reference the id of a view inside an "include" layout in xml在 xml 中的“包含”布局中引用视图的 ID
【发布时间】:2014-02-03 08:36:56
【问题描述】:

我在special_button.xml 内部有一个按钮A,我可以在所有活动中重复使用该按钮。每个活动都有一个根RelativeLayout

问题:我的一个活动有一个按钮,B,与A 位于同一位置。我决定将B 移到A 上方,但我无法从活动的xml 中引用A

这里是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>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您可以像这样将id 属性添加到您的&lt;include&gt;

    <include android:id="@+id/someId"
             layout="@layout/special_button"/>
    

    【讨论】:

    • 我试过这个。这种方法的问题是 id 被赋予了包含布局的根。在我的例子中,它是merge 标签,它实际上是活动的RelativeLayout。我仍然无法访问按钮 A。
    • 您将无法使用此方法从 XML 中直接引用按钮 A,但您现在可以引用您在 &lt;include&gt; 中定义的新 ID。这将适用于您提到的 layout_above 值。
    • 如果使用了合并标签(id、可见性和layout_*标签被忽略),这实际上不起作用,如果没有指定layout_width和layout_height,这也不起作用,请参阅stackoverflow.com/questions/2316465/…
    • @anddev84:通过使用附加到包含的 id,我将引用覆盖整个屏幕的 RelativeLayout。我仍然没有关于如何将按钮 B 放在按钮 A 上方的信息。
    • @eskimoapps.com:谢谢你的链接。尽管 Macarse 的方法更简洁,但我认为它不会解决我的问题。我会尝试使用alienjazzcat 的方法。
    【解决方案2】:

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

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

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

    编辑:正如@eskimoapps.com 指出的那样,使用RelativeLayouts 执行此操作似乎存在一个已知问题,正如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/A 下方 id/B

    我唯一的想法是 Eclipse 不喜欢它,因为它静态地试图在同一个布局文件中找到 Button。由于 &lt;include&gt;&lt;merge&gt; 与 LayoutManager 一起动态工作,所以这可能仍然按预期工作。

    请尝试看看这是否也适合您。

    【讨论】:

    • 我试过这个。这种方法的问题是 id 被赋予了包含布局的根。在我的例子中,它是merge 标签,实际上是活动的RelativeLayout。我仍然无法访问按钮 A。
    • 如果您为&lt;include&gt; 提供的ID 与RelativeLayout 的ID 不同,则您的说法不正确。请检查我的编辑,看看是否有帮助。
    • 哇。谢谢!我不知道你可以直接引用A。只是为了让您知道,我没有在&lt;include&gt; 标签中添加任何数据,只是:&lt;include layout="@layout/special_button" /&gt; 并且它起作用了。
    • 非常感谢!我不知道包含标签的“id”(和其他属性)会覆盖包含布局上的根视图。你的第一段让我大开眼界:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2017-04-08
    相关资源
    最近更新 更多