在LinearLayout中放置3个按钮以占用相等的空间

use*_*152 41 android android-layout

我希望有三个按钮水平连续占用相同数量的可用空间.我用过android:layout_gravity.问题是什么?

layout xml:

<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="1.0"
    >

    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bg"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:layout_gravity="left"
    />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Bm"
            android:background="@drawable/button_red"
            android:layout_weight=".2"
            android:textColor="#ffffff"
            android:layout_gravity="center"
    />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_red"
        android:layout_weight=".2"
        android:text="@string/Bf"
        android:layout_gravity="right"
    />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果有人看到什么错,谢谢.

Ron*_*nie 110

以下布局应该有效.权重适用于LinearLayouts ..

<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="3"
>

    <Button android:id="@+id/button1"
            ...
            android:layout_weight="1"/>

    <Button android:id="@+id/button2"
            ...
            android:layout_weight="1"/>

    <Button
        android:id="@+id/button3"
        ...
        android:layout_weight="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


小智 12

除了设置的layout_weight,你必须设置layout_widthlayout_height0dp.因此,如果您想要水平分配按钮,则layout_width应该是0dp和layout_weight.2或任何数字,只要通过您拥有的按钮相等即可.

所以你的布局应该是这样的

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bg"
        android:background="@drawable/button_red"
        android:layout_weight="1"
/>
<Button android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/Bm"
        android:background="@drawable/button_red"
        android:layout_weight="1"
        android:textColor="#ffffff"
/>

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/button_red"
    android:layout_weight="1"
    android:text="@string/Bf"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)