【问题标题】:How to set properly the button size in a LinearLayer in Android?如何在 Android 的 LinearLayer 中正确设置按钮大小?
【发布时间】:2011-10-11 19:45:04
【问题描述】:

我有以下问题:

我有一个带有两个按钮的水平线性布局,确定和取消。我希望按钮填充父级的宽度,我的意思是每个按钮 50%(如果我有 3 个按钮,每个按钮 33%),按钮必须具有相同的宽度但是当我在确定按钮中选择“填充父级”时,它会填充所有内容并取消按钮不再可见。

感谢您的帮助。非常感谢

【问题讨论】:

    标签: android button android-linearlayout alignment


    【解决方案1】:

    在 xml 布局中为每个按钮添加这个标签:

    android:layout_weight="1"
    

    并将按钮宽度设置为 0px

    【讨论】:

    • 设置 0px 为按钮宽度或按钮 layout_width 属性?
    【解决方案2】:

    使用weightSumlayout_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。

    【讨论】:

    • 非常感谢,请投票我试图达到投票所需的最低声望的问题
    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 2014-09-20
    • 1970-01-01
    • 2017-10-15
    • 2015-08-01
    • 1970-01-01
    • 2011-12-06
    • 2016-09-18
    相关资源
    最近更新 更多