2012-11-30 64 views
2

我試圖讓三個按鈕彼此相鄰,左邊的一個和右邊的一個是小的,中間的一個應該填滿寬度的其餘部分 - 2x2dp(= margin) 。 我對以下代碼進行了編碼,但它不斷推動我的右鍵離開屏幕,關於如何解決此問題的任何想法?可能有辦法讓左邊的和右邊的都有效?謝謝!RelativeLayout:按鈕將其他按鈕從屏幕上移開

<RelativeLayout 
      android:id="@+id/rl" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="10dp" > 

      <Button 
       android:id="@+id/bPreviousQuestion" 
       android:layout_width="42dp" 
       android:layout_height="42dp" 
       android:background="#c8c8c8" 
       android:text="&lt;" 
       android:textColor="#ffffff" 
       android:textSize="20dp" 
       /> 

      <Button 
       android:id="@+id/bBack" 
       android:layout_width="fill_parent" 
       android:layout_height="42dp" 
       android:layout_toRightOf="@+id/bPreviousQuestion" 
       android:background="#c8c8c8" 
       android:text="Go Back" 
       android:textColor="#ffffff" 
       android:textSize="20dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="2dp" /> 

      <Button 
       android:id="@+id/bNextQuestion" 
       android:layout_width="42dp" 
       android:layout_height="42dp" 
       android:layout_toRightOf="@+id/bBack" 
       android:background="#c8c8c8" 
       android:text="&gt;" 
       android:textColor="#ffffff" 
       android:textSize="20dp" /> 
     </RelativeLayout> 

回答

3

嘗試增加:

android:layout_toLeftOf="@+id/bNextQuestion" 

你的 「背」按鈕。在「下一個」刪除此行:

android:layout_toRightOf="@+id/bBack" 

並添加:

android:layout_alignParentRight="true" 
+0

不錯的山姆,謝謝!所以你確實可以通過讓它們取決於你在屏幕上「修復」的其他按鈕來實現按鈕的特權,謝謝! –

+2

是的,通過試驗和錯誤,你會發現哪些屬性優先於其他屬性。例如,'layout_alignParent ____'是一個將視圖固定到某個位置的更加明確的屬性,而'toLeftOf'和'toRightOf'則是動態設計。你還會發現諸如'layout_center _____'這樣的屬性不會改變,它們將以其特定的軸爲中心。 – Sam

0

你的第二按鈕的layout_width是 「FILL_PARENT」

+0

是的,我知道,那就是推動它,但我可以採用某種特權的左,右一個抵靠中間按鈕?還是有另一種方式? –

2

試試這個:

集合{}上左與42dp寬度對準父母。

將{Next}設置爲對齊的父權,寬度爲42dp。

將{Back}設置爲位於Prev的右側,並且設置爲具有fill_parent寬度的Next的左側。

應該如果我正確地正確佈局佈局,用後退按鈕填充按鈕。

+1

感謝馬特,與山姆一樣的解決方案,它的工作原理! :) –

0

你的第二個按鈕是中間按鈕,你正在使用android:layout_width =「fill_parent」。取而代之的是採用Android的:layout_width = 「42dp」,然後你會看到你的第三個

+0

是的,但取決於屏幕的大小,按鈕看起來很奇怪。如果理解正確,OP將希望看到後退按鈕被拉伸以填充Prev和Next按鈕之間的空間。 –

+0

正確的馬特,這是我需要完成 –

+0

然後你需要使用相對佈局內的線性佈局放置在底部。在線性佈局設置佈局:所有三個按鈕的重量 –

1

使用LinearLayout代替RelativeLayout

<LinearLayout 
    android:id="@+id/rl" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="10dp" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/bPreviousQuestion" 
     android:layout_width="42dp" 
     android:layout_height="42dp" 
     android:background="#c8c8c8" 
     android:text="&lt;" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/bBack" 
     android:layout_width="fill_parent" 
     android:layout_height="42dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="#c8c8c8" 
     android:text="Go Back" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/bNextQuestion" 
     android:layout_width="42dp" 
     android:layout_height="42dp" 
     android:background="#c8c8c8" 
     android:text=">" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 
</LinearLayout> 
+0

我需要相對佈局將它放在底部...... :) –

+0

'LinearLayout'可以放在底部! –