我正在尝试在水平LinearLayout中排列两个按钮,以便它们均匀分布,但同时按钮不会延伸到可用空间(中间只有空白区域)。
我正在尝试按照我在this question中找到的内容,特别是安德鲁的评论......
我目前的布局是这样的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
android:minHeight="?android:attr/listPreferredItemHeight">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/db_export_cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Cancel"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:id="@+id/db_export_ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK"
/>
</LinearLayout>
</LinearLayout>
两个LinearLayouts正确使用了可用空间的每一半,但按钮在每个按钮的左侧对齐...
答案 0 :(得分:1)
将每个内部LinearLayouts更改为垂直方向。然后每个人都应该注意其封闭按钮的水平重力。
答案 1 :(得分:0)
每个按钮周围的LinearLayouts有什么意义?只需在各个按钮上设置重量并摆脱中间人。然后,如果你想让按钮变小,你可以a)向按钮本身添加一些左右填充,或者b)在按钮的左侧和右侧添加两个间隔视图,每个视图的权重为1 。