gpt4 book ai didi

android - 为什么这些按钮的尺寸没有重量?

转载 作者:行者123 更新时间:2023-11-29 21:13:25 24 4
gpt4 key购买 nike

我想我在使用 layout_weight 时遇到了问题。我试图使第四个按钮的大小是其他按钮的两倍,但我得到以下信息:

enter image description here

我的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">

<!--android:background="@drawable/button_image"-->

<Button android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.2" />

<Button android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2" />

<Button android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2" />

<Button android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4" />

</LinearLayout>

权重是否应该满足我的要求?

最佳答案

如果你想强制比例作为一个分数,使按钮的layout_width = 0,以及布局的weightSum这包含它们等于它们的权重之和,虽然这最后一步不是必需的,但如果有 subview 未指定它们的权重,则将使用它从余数计算它。

请注意,如果您不将它们设置为 0,则宽度将是 wrap_contentweight 的宽度的乘积,这会给您带来不精确的比例.


布局:

    <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_weight="1"
android:text="1" />

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

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

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

结果:

enter image description here

关于android - 为什么这些按钮的尺寸没有重量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com