2012-04-17 61 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