-1

我試圖將按鈕居中在其他兩個按鈕中間。我嘗試使用weight,但沒有運氣(Android Studio一直說有錯誤),使用layout_marginRight不是一個好主意,因爲它不是獨立於分辨率的。 這裏是我的XML代碼我希望它能幫助你理解我的問題。其他兩個按鈕之間的中心按鈕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/app_bar_main" 
tools:context=".MainActivity"> 

<TextView 
    android:text="Hello World!" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_bottom" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:background="?attr/colorPrimary" 
    android:layout_alignParentBottom="true" 
    android:minHeight="?attr/actionBarSize" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="center"> 

     <RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

      <Button 
       android:id="@+id/bottom_LeftButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Lft" 
       android:layout_alignParentLeft="true" /> 

      <Button 
       android:id="@+id/bottom_LeftCenterButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="LftC" 
       android:layout_alignParentLeft="true" /> 

      <Button 
       android:id="@+id/bottom_CenterButtom" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:text="Cnt" /> 

      <Button 
       android:id="@+id/bottom_RightCenterButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="RhtC" 
       android:layout_alignParentRight="true" /> 

      <Button 
       android:id="@+id/bottom_RightButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Rht" 
       android:layout_alignParentRight="true" /> 

     </RelativeLayout> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

+0

怎麼樣使用的LinearLayout體重? – UpmostScarab

+1

使用'linearLayout'。 –

+0

您不能在RelativeLAyouts中使用權重。 –

回答

2
<LinearLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/bottom_LeftButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Lft" 
      android:layout_weight="1" 
      android:layout_alignParentLeft="true" /> 

     <Button 
      android:id="@+id/bottom_LeftCenterButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="LftC" 
      android:layout_weight="1" 
      android:layout_alignParentLeft="true" /> 

     <Button 
      android:id="@+id/bottom_CenterButtom" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="Cnt" /> 

     <Button 
      android:id="@+id/bottom_RightCenterButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="RhtC" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" /> 

     <Button 
      android:id="@+id/bottom_RightButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Rht" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" /> 

    </LinearLayout> 
+0

這就像一個我不知道RelativeLayout和Weight的魅力。每個按鈕現在都有不同的尺寸,我怎樣才能將它們五個尺寸相同? – user3320009

+0

刪除android:layout_alignParentRight =「true」並將所有按鈕中的權重設置爲3 @ user3320009 – YUVRAJ

+0

工作!非常感謝。 – user3320009

0

使用線性佈局則包括「重量」到你的按鈕,因爲體重屬於LinearLayout中。

2

要保持所有按鈕的大小相同,您需要將width屬性保持爲「fill_parent」。下面是工作的XML。權重應該等於您想要使用的按鈕的數量。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="4"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 


    </LinearLayout> 

+0

三個按鈕保持權重爲3 –

+0

'weightSum'實際上**無用**。和'fillParent'已被**棄用**和API等級8。 –

+0

好吧,你可以用match_parent @ FrankN.Stein替換fill_parent。即使文本長度增加,該解決方案仍然保持元素的寬度相同 –

相關問題