线性布局中的按钮重量

时间:2015-06-08 00:28:14

标签: 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和第二个这个宽度为第三个新

所以它看起来像这样:

buttons2

我能做到的唯一方法是将权重设为0.93 / 0.07

不应该使用权重2/1,因为上面的代码是?

2 个答案:

答案 0 :(得分:2)

尝试遗漏android:weightSum="3"第二行按钮。省略weightSum时,默认情况下总计视图的权重,它们基本上成为直线比例(比率为2比1)。因此,只使用weight = 2作为第一个按钮(id button5),然后weight = 1表示第二个按钮(id button7)应该可以工作。

此外,在使用layout_weight时,请确保您的可调整大小的视图的layout_widthlayout_height等于零。

试试这个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>

答案 1 :(得分:1)

这是我写的方式,你想要的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


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

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

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="New "
                android:id="@+id/button1"
                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" />

        </LinearLayout>

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

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

    </LinearLayout>

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

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

    </LinearLayout>

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

    </LinearLayout>

</LinearLayout>

这是结果

enter image description here

我只有一件事我不确定,为什么在我的上一次LinearLayout中我必须加权= 6才能让按钮处于那个高度。我正在寻找原因:/

相关问题