在另一个XML文件中引用View

时间:2011-04-12 15:03:37

标签: android layout android-layout

我在名为a.xml的文件中有一个ListView。

<ListView
        android:id="@+id/mylistview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scrollbars="none"
        android:layout_above="XYZ"
        />

在a.xml中,我还包含另一个名为b.xml的文件。

<include android:id="@+id/bottombar" layout="@layout/b" />

对于ListView的其中一个设置,我想引用b.xml中存在的id(XYZ)。

我有办法做到这一点吗?

我尝试了以下内容:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include android:id="@+id/bottombar" layout="@layout/b" />
    <ListView
        android:id="@+id/mylistview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scrollbars="none"
        android:layout_above="@id/bottombar_background"
        />
</RelativeLayout>

@ layout / b中存在@ id / bottombar_background,但Eclipse会向我抛出“无资源”错误。

2 个答案:

答案 0 :(得分:1)

使用setContentView(R.layout.a)后,您应该可以使用findViewById(R.id.my_id_in_b)

答案 1 :(得分:0)

使用以下语法:android:layout_toLeftOf =“@ + id / my_id_from_b”。它可以防止任何参考问题。

此实例中的加号告诉编译器在布局上进行第二次传递,第二次查找该id。尝试一下 - 我知道它有效,因为我一直使用这种语法。

相关问题