LinearLayout中按钮之间的相等空间

时间:2015-08-26 15:28:54

标签: android android-layout button android-linearlayout

我在LinearLayout中有四个垂直排列的按钮。

buttons

我希望按钮之间的空间和最顶部按钮和线性布局顶部之间的空间以及最底部按钮和布局底部之间的空间是相同的。

在附图中,空格被描绘为红色路径。 我希望所有的空间都是相同的大小。

我将如何实现目标?

<LinearLayout
            p1:orientation="vertical"
            p1:layout_width="wrap_content"
            p1:layout_height="wrap_content"
            p1:id="@+id/mainButtonLayout">
            <Button
                p1:text="xxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/saButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/rButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/sButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp"
                p1:layout_marginBottom="20dp" />
            <Button
                p1:text="xxxxx"
                p1:layout_width="match_parent"
                p1:layout_height="wrap_content"
                p1:id="@+id/pButton"
                p1:textColor="#FFFFFF"
                p1:background="@drawable/roundedBlue"
                p1:minHeight="33dp"
                p1:minWidth="175dp" />
        </LinearLayout>

3 个答案:

答案 0 :(得分:0)

将按钮放在布局的中心:

<LinearLayout
    android:orientation="vertical"
    android:gravity="center_vertical"
    ... >

要在按钮之间放置空格,我可以想到两个选项。第一个是在除第一个按钮之外的所有按钮上使用android:layout_marginTop。第二种是使用硬编码的大小在XML中绘制一个空的形状,并将其用作LinearLayout的分隔符

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size android:width="your_dimension_here"
        android:height="your_dimension_here" />

    <solid android:color="@android:color/transparent" />
</shape

<LinearLayout
    android:divider="@drawable/divider_space"
    android:showDividers="middle"
    ... >

答案 1 :(得分:0)

在您的视图之间使用它,

<View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

答案 2 :(得分:-1)

只需将p1:gravity =“center”添加到线性布局中。

<LinearLayout
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:orientation="vertical"
p1:gravity="center"
p1:id="@+id/mainButtonLayout">
相关问题