【发布时间】:2011-10-11 19:45:04
【问题描述】:
我有以下问题:
我有一个带有两个按钮的水平线性布局,确定和取消。我希望按钮填充父级的宽度,我的意思是每个按钮 50%(如果我有 3 个按钮,每个按钮 33%),按钮必须具有相同的宽度但是当我在确定按钮中选择“填充父级”时,它会填充所有内容并取消按钮不再可见。
感谢您的帮助。非常感谢
【问题讨论】:
标签: android button android-linearlayout alignment
我有以下问题:
我有一个带有两个按钮的水平线性布局,确定和取消。我希望按钮填充父级的宽度,我的意思是每个按钮 50%(如果我有 3 个按钮,每个按钮 33%),按钮必须具有相同的宽度但是当我在确定按钮中选择“填充父级”时,它会填充所有内容并取消按钮不再可见。
感谢您的帮助。非常感谢
【问题讨论】:
标签: android button android-linearlayout alignment
在 xml 布局中为每个按钮添加这个标签:
android:layout_weight="1"
并将按钮宽度设置为 0px
【讨论】:
使用weightSum 和layout_weight 属性来创建所需的视图。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"
android:weightSum="2">
<Button android:text="Ok" android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" />
<Button android:text="Cancel" android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
如果是三个按钮,weightSum 是 3。
【讨论】: