【问题标题】:Two views with the same weight in LinearLayoutLinearLayout中具有相同权重的两个视图
【发布时间】:2013-06-30 17:53:50
【问题描述】:

在下面的代码中,我在 LinearLayout 中使用 layout_weight=1 和 layout_width(或 layout_height)等于 0 的组合。 一个问题是为什么在第二个 LinearLayout 中,第二个按钮和第一个按钮不占用相等的空间?它们具有相同的权重 (layout_weight=1),因此它们应该具有相同的空间。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:text="wreoiwe roiwej roiwejr weoirjweoirjwoeirjoweijrowerjowejorjweoirjwoeiwoi" >
    </TextView>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/Button01"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="Submit" >
        </Button>

        <Button
            android:id="@+id/Button02"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="Cancelrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr" >
        </Button>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    这是因为第二个LinearLayoutwrap_content,而layout_weight 不是这样工作的。

    来自文档:

    较大的权重值允许它扩展以填充父视图中的任何剩余空间。

    您的LinearLayout wrap_content 没有填满整个屏幕,父视图中不再有“剩余空间”。一切都与它应该的一样大。它不会展开较小的按钮,因为父级位于wrap_content,没有更多空间可以展开。

    您可以通过将第二个 LinearLayout 更改为 match_parent 来测试这一点,您将看到每个按钮占据屏幕的一半。

    【讨论】:

      【解决方案2】:

      如果你想让 textview 有一行,下一行两个 bottons 占据总宽度的 50%,你必须放在第二个线性布局上

      android:layout_width="fill_parent"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 2019-01-13
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 2019-10-28
        相关资源
        最近更新 更多