线性布局中的按钮重量
Button weight in linear layout
这是我的代码
<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>
这是它的样子:
我希望第一个 This 的宽度与前两个 New 相同,第二个 This 的宽度与第三个 New 相同
所以看起来像这样:
我能做到的唯一方法是将权重设置为 0.93/0.07
它不应该像上面的代码那样使用权重 2/1 吗?
尝试为第二行按钮省略 android:weightSum="3"
。当您省略 weightSum 时,默认情况下视图的权重总和,它们基本上变成直线比例(比率如 2 比 1)。因此,只需对第一个按钮 (id button5) 使用 weight=2,然后对第二个按钮 (id button7) 使用 weight=1 就可以了。
此外,在使用 layout_weight
时,请确保您的可调整大小视图的 layout_width
或 layout_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>
我就是这样写的,有你想要的布局
<?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>
这是结果
只有一件事我不确定,这就是为什么在我上一个 LinearLayout 中我必须将 weight = 6 设置为那个高度的按钮。我正在寻找原因:/
这是我的代码
<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>
这是它的样子:
我希望第一个 This 的宽度与前两个 New 相同,第二个 This 的宽度与第三个 New 相同
所以看起来像这样:
我能做到的唯一方法是将权重设置为 0.93/0.07
它不应该像上面的代码那样使用权重 2/1 吗?
尝试为第二行按钮省略 android:weightSum="3"
。当您省略 weightSum 时,默认情况下视图的权重总和,它们基本上变成直线比例(比率如 2 比 1)。因此,只需对第一个按钮 (id button5) 使用 weight=2,然后对第二个按钮 (id button7) 使用 weight=1 就可以了。
此外,在使用 layout_weight
时,请确保您的可调整大小视图的 layout_width
或 layout_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>
我就是这样写的,有你想要的布局
<?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>
这是结果
只有一件事我不确定,这就是为什么在我上一个 LinearLayout 中我必须将 weight = 6 设置为那个高度的按钮。我正在寻找原因:/