在LinearLayout中放置3个按钮以占用相等的空间 [英] Place 3 buttons in a LinearLayout to occupy equal amount of space

查看:75
本文介绍了在LinearLayout中放置3个按钮以占用相等的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让三个按钮连续水平地占据相等的可用空间. 我使用了android:layout_gravity.有什么问题吗?

i would like to have three buttons taking equal amount of available space horizontally in a row. I used android:layout_gravity. What is the problem?

布局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>

如果有人看到错了,谢谢.

if someone see whats wrong, thanks.

推荐答案

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

The following layout should work. weights are for 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>

这篇关于在LinearLayout中放置3个按钮以占用相等的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆