2011-02-17 66 views
3

在我的Android應用程序中,我在一個單獨的XML文件中聲明瞭一個自定義視圖。包含的設置佈局屬性

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/download_interval_setter" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"> 
<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/Button_interval_up" 
    style="?android:attr/buttonStyleSmall"> 
</Button> 
<EditText 
    android:id="@+id/Download_interval" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    android:numeric="integer"> 
</EditText> 
<Button 
    android:id="@+id/Button_interval_down" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
</Button> 
</LinearLayout> 

然後,我有我的main.xml文件,我想包括在上面的XML聲明如下觀點:

<?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"> 
<ToggleButton android:layout_height="wrap_content" 
     android:id="@+id/XMLStart" 
     android:textOn="@string/auto_update_on" 
     android:textOff="@string/auto_update_off" 
     android:layout_width="wrap_content" /> 
<include layout="@layout/download_interval_setter" 
     android:layout_toRightOf="@id/XMLStart"/> 
<Button android:layout_height="wrap_content" 
     android:id="@+id/ShowRadars" 
     android:text="@string/show_radars" 
     android:layout_width="wrap_content" 
     android:layout_below="@id/XMLStart"/>  
<Button android:layout_height="wrap_content" 
     android:id="@+id/ShowAccidents" 
     android:text="@string/show_accidents" 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@id/ShowRadars" 
     android:layout_below="@id/XMLStart"/> 
<Button android:layout_height="wrap_content" 
     android:id="@+id/ShowConstraints" 
     android:text="@string/show_constraints" 
     android:layout_width="wrap_content" 
     android:layout_below="@id/ShowRadars"/>  
<Button android:layout_height="wrap_content" 
     android:id="@+id/ShowPatrols" 
     android:text="@string/show_patrols" 
     android:layout_width="wrap_content" 
     android:layout_below="@id/ShowRadars" 
     android:layout_toRightOf="@id/ShowConstraints"/>  
</RelativeLayout> 

正如你可以看到,我想設置的layout_toRightOf屬性所包含的觀點,但它沒有設置,因爲我還可以看到在hierarchyviewer

在另一方面,如果我只是複製粘貼download_interval_setter.xml文件的內容的main.xml和申報layout_toRightOf參數那裏,視圖將定位在我的右側想。

我哪裏錯了? 感謝

回答

0

您必須在所包含的XML文件中使用RelativeLayout作爲根佈局,因爲android:layout_toRightOf不能LinearLayout使用。

+0

不,不是的。我嘗試過,與LinearLayout一樣,除此之外,正如我所提到的,如果我只是將LinearLayout複製粘貼到main.xml中並向粘貼的LinearLayout添加android:layout_toRightOf屬性,則它將被正確定位。它只與它不起作用 – durfejs 2011-02-17 13:31:23

+0

對不起,這是我的錯誤。你的xml似乎是正確的。 – Michael 2011-02-17 13:39:18