2012-07-10 33 views
1

我想要一個TextView來填充左邊的按鈕和右邊佈局的邊緣之間的空間。相對佈局的怪異邊距問題

<TextView 
    android:id="@+id/welcomeHeader" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 
    android:layout_toRightOf="@+id/setInfusionReminder" 
    android:layout_marginRight="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    android:gravity="center" 
    android:text="Welcome!" /> 

我在編輯器中看到的是android:layout_marginRight應用在左邊。最終結果是左側40dp餘量和右側0餘量。

enter image description here

+0

您是否嘗試過使用'android:layout_gravity =「center」'? – b3by 2012-07-10 14:35:36

回答

1

這是下面的屬性不很一起工作,你會期望:

android:layout_toRightOf="@id/setInfusionReminder" 
android:layout_alignParentRight="true" 

旁註:ATLEAST從我在我自己的項目見過,也容易使元素在某種程度上無法控制,以適應更多與佈局相關的屬性。

更新來解決註釋:

我可能會做到以下幾點:

1. Remove the android:layout_alignParentRight="true" 
2. Set the android:layout_width to "fill_parent" 
3. Keep the gravity at android:gravity => "center" 

這應該使它儘可能向右展開,而中心內容(文本)。

+0

問題是爲什麼以及如何使其按預期工作? – 2012-07-10 14:48:05

+0

我已經更新了我上面的答案。 – ninetwozero 2012-07-10 14:52:00

+0

它現在按預期工作嗎? :) – ninetwozero 2012-07-10 14:56:07

0

我想你同時使用了android:layout_alignParentRight="true"android:layout_toRightOf="@+id/setInfusionReminder"。刪除android:layout_alignParentRight並嘗試它應該如你所願。

 <TextView 
       android:id="@+id/welcomeHeader" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_toRightOf="@+id/btnSettkoff" 
       android:layout_marginRight="20dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="20dp" 
       android:gravity="center" 
       android:text="Welcome!" /> 

希望它有幫助。

+0

我想將文本居中放置在按鈕和右邊之間的空白處。刪除'android:layout_alignParentRight'會使文本粘在按鈕上。 – 2012-07-10 14:44:17

+0

我更新了爲我工作的答案(測試)..試試這個 – GoCrazy 2012-07-10 14:54:11