三个按钮均匀划分线性布局

时间:2016-07-05 02:23:34

标签: android button android-linearlayout

所以,我试图使用线性布局来容纳三个不同的按钮,每个按钮占据一行宽度的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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

这可能会对你有帮助。

答案 2 :(得分:0)

试试这个,在你的LinearLayout中添加android:gravity =&#34; center&#34;

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center">
...
</LinearLayout>
相关问题