【问题标题】:Why does Android layout weight and wrap_content give more space than needed?为什么 Android 布局权重和 wrap_content 提供了比需要更多的空间?
【发布时间】:2013-09-05 14:20:28
【问题描述】:

我一直在玩弄 Android 布局,试图更好地理解权重,但我偶然发现了一个我无法解释的场景。我连续三个 UI 组件。一个权重为 1 且宽度为 wrap_content 的 LinearLayout(其中有一个按钮),后跟两个宽度为 0dp 且权重为 1 的按钮。我原以为 LinearLayout 会小于按钮的宽度,但是当它是渲染后,LinearLayout 占据了一半的空间,每个按钮占据了四分之一的屏幕空间。即使第一个按钮(在 LinearLayout 内)比线性布局使用的空间小得多,也会发生这种情况。谁能解释一下为什么?

PS:我知道如果您将 LinearLayout 的宽度设置为 0dp,重量为 1,这可以正常工作(所有等距),但想知道为什么这种情况会导致它的结果。

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

    <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_bright">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="W =  1"/>
    </LinearLayout>

    <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="W =  1"/>
    <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="W =  1"/>
</LinearLayout>          

【问题讨论】:

  • 你正在为布局设置权重

标签: android android-layout layout


【解决方案1】:

我不确定我是否能清楚地解释为什么这不起作用,除非你使用weight,那么你的View 应该有widthheight 0 取决于其父级的orientation。对不起,我更擅长举例而不是言语。但是,我可以告诉你,要达到你想要的效果,只需从子LinearLayout 中取出layout_weight 属性,你就会得到想要的效果。

这会将LinearLayout 包装到它需要的位置,然后让两个Buttons 平均占用其余空间。试一试,你就会明白我的意思。我认为有一篇文章谈到了这一点。我看看能不能找到。

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

<LinearLayout
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        android:layout_height="wrap_content"  // removed weight so the LL will just wrap the content
        android:background="@android:color/holo_blue_bright">

找到了

layout_weight这部分文档有一个你正在尝试做的例子。

【讨论】:

    【解决方案2】:

    我认为您不需要像其他人所说的那样定义 weightSum。

    我测试了您的代码,实际上您的发现非常有趣。似乎正在发生的事情是内部 LinearLayout 首先使用包装内容来获取其基本宽度,然后在自身和其他两个按钮之间共享剩余宽度。因此,内部按钮很小(仅使用其最小宽度),LinearLayout 的宽度等于小按钮 + 其他按钮之一。

    我希望这是有道理的。不错的发现!

    【讨论】:

    • 确实,重量总和没有任何区别。你能多解释一下基本宽度吗,我真的很好奇这个宽度是怎么计算出来的。
    • 'base' 宽度是 'wrap_content' 采用的宽度,因此它是视图的子视图(在本例中为按钮)所需的最小宽度。因此,例如,如果您更改按钮的文本,您将看到父级的宽度与我上面描述的成比例变化。
    【解决方案3】:

    您正在将权重设置为 LinearLayout 所以它占用了所有可用空间

    而且您也没有将 weight_sum 定义给任何父级

     <LinearLayout
            android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W =  1"/>
        </LinearLayout>
    

    注意:

    重量是相对于屏幕的宽度/高度而不是相对于父母的宽度/高度 所以减轻重量

    如果你设置 weight_sum = "10" 那么整个屏幕可以分成 10 个部分,你可以根据需要使用layout_weight

    weight_sum

    定义最大重量总和。如果未指定,则通过添加所有子项的 layout_weight 来计算总和。例如,这可以通过将 layout_weight 设置为 0.5 并将 weightSum 设置为 1.0 来为单个子项提供总可用空间的 50%。

    必须是浮点值,例如“1.2”。

     <LinearLayout
            android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_height="wrap_content"
                android:background="@android:color/holo_blue_bright">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W =  1"/>
        </LinearLayout>
    

    更多关注this

    你也可以参考this

    【讨论】:

      【解决方案4】:

      你需要定义

      android:weightSum="" // 3 in your case
      

      在你的外部线性布局中

      【讨论】:

        猜你喜欢
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-29
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多