我在 LinearLayout 中垂直排列了四个按钮。
我希望按钮之间的间距与最顶部按钮与线性布局顶部之间的间距以及最底部按钮与布局底部之间的间距相同。
在附图中,空间被描绘为红色路径。 我希望所有空间的大小都相同。
我将如何实现我的目标?
<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/