android - 线性布局中的按钮重量

标签 android android-layout android-layout-weight

这是我的代码

        <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.75">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button"
                    android:layout_weight="1"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button2"
                    android:layout_weight="1"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button3"
                    android:layout_weight="1"
                    android:background="#ffffffff" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="3"
                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button5"
                    android:layout_weight="2"
                    android:background="#ffffffff" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New "
                    android:id="@+id/button7"
                    android:layout_weight="1"
                    android:background="#ffffffff" />
            </LinearLayout>

        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:text="New "
            android:id="@+id/button4"
            android:background="#ffffffff" />
    </LinearLayout>

这就是它的样子:

buttons

我希望第一个This的宽度与前两个New相同,第二个This的宽度与第三个New相同

所以它看起来像这样:

buttons2

我能做到这一点的唯一方法是将权重放在 0.93/0.07

它不应该像上面的代码那样使用权重 2/1 吗?

最佳答案

尝试省略第二行按钮的 android:weightSum="3" 。当您省略weightSum时,默认情况下 View 的权重会被合计,并且它们本质上变成直线比例(例如2比1的比率)。因此,只需对第一个按钮(id button5)使用weight=2,然后对第二个按钮(idbutton7)使用weight=1就可以了。

此外,使用 layout_weight 时,请确保可调整大小的 View 的 layout_widthlayout_height 等于 0。

试试这个 xml:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button"
                android:layout_weight="1"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button2"
                android:layout_weight="1"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button3"
                android:layout_weight="1"
                android:background="#ffffffff" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button5"
                android:layout_weight="2"
                android:background="#ffffffff" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button7"
                android:layout_weight="1"
                android:background="#ffffffff" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.25"
        android:text="New "
        android:id="@+id/button4"
        android:background="#ffffffff" />
</LinearLayout>

关于android - 线性布局中的按钮重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699823/

相关文章:

java - 解码旋转的位图

android - Looper线程问题?但不使用 Animator

android - 图像和文本居中的 GridView

Android TextView 不会扩展以匹配父级

android - match_parent 在自定义工具栏中不起作用

java - 移动项目文件夹后应用程序崩溃 android studio

android - ConstraintLayout 内的 ListView - 项目居中

Android、setText 和 setTypeface 不起作用

android - 如何使我的应用程序全屏显示?

android - layout_weight 不一致