2014-06-11 152 views
-3

我在android:layout_toRightOf有問題Invalid layout param 有沒有辦法將此參數添加到項目中?Android xml佈局:toRightOf

是這樣的代碼

<TextView 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/text2" 
    android:background="@color/red"/> 
+0

使用RelativeLayout的? – Raghunandan

+0

no linearLayout – user3100088

+2

'LinearLayout'沒有該屬性。使用'RelativeLayout'。澈文檔。如果你不想發佈這個問題 – Raghunandan

回答

3

您需要文本1和文本到同父RelativeLayout的同時爲兒童。

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/text2" 
     android:background="@color/red"/> 
    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 
+0

它不適用於LinearLayout嗎? – user3100088

+0

如果您使用的是LinearLayout,則不能使用toRightOf ...僅用於RelativeLayouts的屬性。 線性佈局如名稱所示,以水平或垂直順序放置對象。 RelativeLayouts是將對象與其父對象或另一個對象相關聯的對象。 –

+1

不,它不會。您不能在線性佈局中使用toRightOf。 「右」或「左」是相對的 - 這就是爲什麼它被稱爲相對佈局。 text1的位置相對於text2。 –

0

您不能在LinearLayout中使用此layout_toRightof屬性。

您使用的是相對佈局,因爲它的名稱是相對的,所以您必須定義您的text1相對於另一個視圖的位置可能是text2。所以定義text1的佈局

android:layout_toRightof="@+id/text2" 

,並宣佈另一個文本2爲

<TextView 
    android:id="@+id/text2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>