这是我的代码
<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时,默认情况下 View 的权重会被合计,并且它们本质上变成直线比例(例如2比1的比率)。因此,只需对第一个按钮(id button5)使用weight=2,然后对第二个按钮(idbutton7)使用weight=1就可以了。
此外,使用 layout_weight
时,请确保可调整大小的 View 的 layout_width
或 layout_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/