2016-07-05 267 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

这可以帮助你。