android - LinearLayout 中按钮之间的间距相等

标签 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>

最佳答案

在 LinearLayout 中使用 weightsum

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

在每个按钮中放置 layout_height = 0dp , layout_weight = 1, top and bottom margin 根据你的需要但每个按钮都一样。

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

了解更多信息:read this document

关于android - LinearLayout 中按钮之间的间距相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230742/

相关文章:

android - 如何制作布局以填充空间直到底部的另一个布局

javascript - 如何根据jsp中的条件禁用按钮?

android - 通过pyqtdeploy和Qt5将PyQt5应用部署到Android

android - 网页适合所有安卓设备

android - Firebase signInWithEmailAndPassword 给出 "com.google.firebase.auth not found"

Android Chrome 转换按钮

html - 按钮 CSS 样式不适用于 iOS

android - Ant 使用ADT的 "build performance improvements"

java - Android 对话框错误指定的 child 已经有一个 parent 。您必须先在 child 的 parent 上调用 removeView()

android - 如何减少 Android 布局中文本之间的行距?