我正在尝试将现有布局包含在另一个布局文件 (xml) 中。要定位这些布局,我需要为包含的布局提供一个 id,以便我能够引用它,例如
我正在使用以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
...
</RelativeLayout>
我将此布局包含在另一个布局中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container"
android:background="@drawable/bg"
>
<include android:id="@id/bar" layout="@layout/bar"/>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bar"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
...
</LinearLayout>
</ScrollView>
</RelativeLayout>
这工作正常,我的 ScrollView 位于我的栏下方,我可以单击栏,而单击事件不会被 ScrollView 拦截。
当我尝试在不同的布局文件中执行相同操作时,它说它找不到资源@id/bar:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layoutContainer2"
>
<include android:id="@id/bar" layout="@layout/bar"/>
<ScrollView
android:id="@+id/scrollview2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/bar"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
...
</LinearLayout>
</ScrollView>
</RelativeLayout>
有人知道这是为什么吗?