LinearLayout中按钮之间的等距间隔

问题描述 投票:1回答:5

我在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>
android android-layout button android-linearlayout
5个回答
5
投票

在您的LinearLayout中使用weightsum

<LinearLayout
...
android:weightSum="4.0">`

在每个按钮中layout_height = 0dp,layout_weight = 1,顶部和底部边距根据您的需要,但每个按钮中的边距都相同。

<Button....
...
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_marginBottom="40dp"
android:layout_marginTop="40dp"
/>

有关更多信息:read this document


1
投票

将按钮放置在布局的中央:

<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"
    ... >

0
投票

希望我会尽力完成您的要求。试试这个xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >
   <Button
        android:id="@+id/btn1"
        android:layout_width="50dp"
        android:layout_height="50dp"
         />

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

    <Button
        android:id="@+id/btn2"
        android:layout_width="50dp"
        android:layout_height="50dp"
       />

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

    <Button
        android:id="@+id/btnTwitter"
        android:layout_width="50dp"
        android:layout_height="50dp"
        />

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

    <Button
        android:id="@+id/btn3"
        android:layout_width="50dp"
        android:layout_height="50dp"
         />
</LinearLayout>

0
投票

在您的视图之间使用它,

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

-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">
© www.soinside.com 2019 - 2024. All rights reserved.