2016-07-05 269 views
-1

所以,我試圖用一個線性佈局來保存三個不同的按鈕,每個按鈕在一條線上應占用33%的寬度。此線性佈局將低於我的相對佈局中的所有其他內容,該佈局包含此活動中的所有其他小部件。不幸的是,當我將第三個按鈕添加到佈局中時,另外兩個按鈕的底部有一個白色條,第三個按鈕(在這種情況下爲主頁按鈕)的位置高於其他按鈕。三個按鈕均勻分佈線性佈局

有人可以解釋這種行爲,以及如何糾正它?謝謝。

這是線性佈局的XML文件,我已經刪除了其他小部件的所有文本。如果這會有所幫助,我也可以發佈。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight=".33" 
     android:layout_height="wrap_content" 
     android:text="@string/adfazsdfasdfadfsagjlfkdlgjklfsadgfjgps" 
     android:onClick="resetDates" 
     android:background="@drawable/sumbitstyleing" 
     android:id="@+id/resetDatesButton" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight=".33" 
     android:layout_height="wrap_content" 
     android:text="@string/home" 
     android:onClick="home" 
     android:id="@+id/homeButtonSearch" 
     android:background="@drawable/generalbutton" /> 

    <Button 
     android:layout_weight=".33" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/submitchanges" 
     android:onClick="submitChanges" 
     android:background="@drawable/sumbitstyleing" 
     android:id="@+id/submitchanges" /> 

</LinearLayout> 

第一張照片沒有第三個按鈕,第二張照片是第三個按鈕。

Without the home button

With the home button

+0

請勿使用alignParentEnd和alignParentRight。首先,這兩個將在LTR語言中發生衝突。其次,由於您的寬度爲match_parent,因此您將自動對齊到父項的兩側。 (alignParentBottom很好,它不衝突)。 –

+0

將'android:baselineAligned =「false」'添加到'LinearLayout'以使'Button's排隊。我不確定白線是什麼。某種渲染故障?這可能是模擬器還是佈局設計器? –

+0

可能重複[是否有可能在android linearlayout的寬度上均勻分佈按鈕](http://stackoverflow.com/questions/3470420/is-it-possible-to-evenly-distribute-buttons-across-the -width-的-AN-機器人-LINEA) – mbmc

回答

1

試試這個,我已刪除屬性你應該RelativeLayout和不必要與使用LinearLayout和按鈕水平對齊,

android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 

,並與按鈕的高度分配Weightsum到的LinearLayout沿着匹配父母

<?xml version="1.0" encoding="utf-8"?> 

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:weightSum="1"> 

     <Button 
      android:id="@+id/resetDatesButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:onClick="resetDates" 
      android:text="Rese check dates" /> 

     <Button 
      android:id="@+id/homeButtonSearch" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:onClick="home" 
      android:text="home" /> 

     <Button 
      android:id="@+id/submitchanges" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:onClick="submitChanges" 
      android:text="submit changes" /> 

    </LinearLayout> 

</RelativeLayout> 

結果

enter image description here

0

試試這個,在你的LinearLayout添加機器人:比重= 「中心」

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center"> 
... 
</LinearLayout> 
1

在這裏,你可以走這條路:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" android:layout_height="match_parent" 
android:background="@color/white" 
> 
<LinearLayout 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_centerHorizontal="true"/> 
    <!--Buttons --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="3" 
     > 

     <android.support.v7.widget.AppCompatButton 
      android:id="@+id/motor_team_send_sms_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/send_sms" 

      /> 

     <android.support.v7.widget.AppCompatButton 
      android:id="@+id/motor_team_sleep_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/sleep" 

      /> 

     <android.support.v7.widget.AppCompatButton 
      android:id="@+id/motor_team_rise_btn" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/rise" 

      /> 
    </LinearLayout> 

輸出是:

enter image description here

這可以幫助你。