Android 线性布局中按钮之间的相等空间
我有四个按钮垂直排列成一条直线 我希望按钮之间的空间,最上面的按钮和线性布局顶部之间的空间,以及最下面的按钮和布局底部之间的空间,是相同的 在附图中,空间被描绘为红色路径。 我希望所有的空间大小相同 我将如何实现我的目标Android 线性布局中按钮之间的相等空间,android,android-layout,button,android-linearlayout,Android,Android Layout,Button,Android Linearlayout,我有四个按钮垂直排列成一条直线 我希望按钮之间的空间,最上面的按钮和线性布局顶部之间的空间,以及最下面的按钮和布局底部之间的空间,是相同的 在附图中,空间被描绘为红色路径。 我希望所有的空间大小相同 我将如何实现我的目标 <LinearLayout p1:orientation="vertical" p1:layout_width="wrap_content" p1:layout_height="wrap_conte
<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
android:orientation="vertical"
android:gravity="center_vertical"
... >
要将按钮放置在布局的中心,请执行以下操作:
<LinearLayout
android:orientation="vertical"
android:gravity="center_vertical"
... >
只需将p1:gravity=“center”添加到线性布局中
<LinearLayout
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:orientation="vertical"
p1:gravity="center"
p1:id="@+id/mainButtonLayout">
只需将p1:gravity=“center”添加到线性布局中即可
<LinearLayout
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:orientation="vertical"
p1:gravity="center"
p1:id="@+id/mainButtonLayout">
在线性布局中使用权重总和
<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"
... >
<LinearLayout
...
android:weightSum="4.0">`
`
每按一个钮扣
布局高度=0dp,布局重量=1,顶部和底部边距根据您的需要而定,但每个按钮都相同
<Button....
...
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_marginBottom="40dp"
android:layout_marginTop="40dp"
/>
有关更多信息:在线性布局中使用权重总和
<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"
... >
<LinearLayout
...
android:weightSum="4.0">`
`
每按一个钮扣
布局高度=0dp,布局重量=1,顶部和底部边距根据您的需要而定,但每个按钮都相同
<Button....
...
android:layout_height="0dp"
android:layout_weight="1.0"
android:layout_marginBottom="40dp"
android:layout_marginTop="40dp"
/>
欲了解更多信息:希望我能完成您的要求。试试这个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>
希望我能尽力满足您的要求。试试这个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>
在视图之间使用此选项
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
在视图之间使用此选项
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
您的解决方案是否与分辨率无关?@Pétur如果您使用以dp单位指定的尺寸,无论您喜欢哪种按钮间距方法。您的解决方案是否与分辨率无关?@Pétur如果您使用以dp单位指定的尺寸,无论您选择哪种方法来设置按钮之间的间距。这不起作用,它会将按钮向左和向右拉伸,但垂直空间保持不变。这不起作用,它会将按钮向左和向右拉伸,但垂直空间保持不变。