2010-09-01 101 views
2

我想獲得一個線上有三個按鈕。中間最遠的一個,最右側的一個。理想情況下,我希望中心按鈕佔據兩邊之間的所有空間。現在中心(菜單)按鈕只是與左側(prev)按鈕重疊。以下是我現在有:如何在兩個其他按鈕之間居中按鈕?

<RelativeLayout android:layout_marginTop="-50dip" android:gravity="bottom" android:layout_height="wrap_content" android:layout_width="fill_parent"> 
<Button 
android:id="@+id/previous_button" 
android:layout_height="wrap_content" 
android:layout_width="110px" 
android:layout_alignParentLeft="true" 
android:text="@string/previous" 
/> 

<Button 
android:id="@+id/menu" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:scaleType="center" 
android:text="@string/menu" 
/> 

<Button 
android:id="@+id/next_button" 
android:layout_height="wrap_content" 
android:layout_width="110px" 
android:layout_alignParentRight="true" 
android:text="@string/next" 
/>   

回答

5

您應該避免使用像素值的大小。如果您確實想要設置明確的值,請使用dpi單位。

在這種情況下,您應該可以使用簡單的LinearLayout與layout_weights。 layout_weight決定了如何在你的小部件中分配剩餘空間。如果給一個小部件的值爲1,其他0則帶1的小部件將耗盡所有額外空間。

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 
    <Button 
    android:id="@+id/previous_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="0"/> 
    <Button 
    android:id="@+id/menu" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1"/> 
    <Button 
    android:id="@+id/next_button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="0"/> 
</LinearLayout> 
+0

感謝,完美的工作:) – tylercomp 2010-09-01 19:49:25

+0

你變換了的RelativeLayout到LinearLayout中......如果你wan't保持你的RelativeLayout,只是把機器人:layout_toRightOf = 「@ ID/previous_button」(HTTP://開發商.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_toRightOf) – fedj 2010-09-01 20:04:58

+0

@tylercomp非常好,很高興它適合你!在計算器上,如果答案提交回答您的問題,您應該選中答案左邊的複選框將其標記爲asnwer。 – 2010-09-01 20:38:39

相關問題