[英]Equal space between buttons in a LinearLayout
我在 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、頂部和底部邊距,但每個按鈕都相同。
<Button....
...
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_marginBottom="40dp"
android:layout_marginTop="40dp"
/>
了解更多信息: 閱讀這份文件
要將按鈕放置在布局的中心:
<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"
... >
在您的視圖之間使用它,
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
希望我會盡力完成您的要求。 試試這個 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>
只需將 p1:gravity="center" 添加到您的線性布局。
<LinearLayout
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:orientation="vertical"
p1:gravity="center"
p1:id="@+id/mainButtonLayout">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.