2012-04-17 96 views
1

我想在另一个布局文件(xml)中包含现有的布局。要定位这些布局,我需要为包含的布局提供一个ID,以便我能够引用它,例如,Android包括布局文件(参考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拦截。

当我试图做同样的在不同的布局文件它说,它找不到的资源@ ID /条:

<?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> 

任何人都知道这是为什么?

回答

1

变化android:id="@id/bar"android:id="@+id/someId"

+0

THX,这是我没有尝试,因为我想用现有的ID唯一,但@ + ID到目前为止固定它。我仍然好奇,为什么它确实为一个案件起作用,而不是其他案件。 – tenshox 2012-04-17 07:58:38