【问题标题】:Wrapping an RelativeLayout around LinearLayouts在 LinearLayouts 周围包裹一个 RelativeLayout
【发布时间】:2017-08-10 00:46:38
【问题描述】:

我在RelativeLayout 中有一个RelativeLayout 和5 个LinearLayouts。一个LinearLayout代表一行按钮,每个LinearLayout有4个按钮,每个按钮都有定义的宽度和高度。

我的问题是,当我将RelativeLayout 的高度设置为wrap_content 时,它不会包裹LinearLayout 的行,而是像fill_parent。有趣的是,它可以将RelativeLayout 的宽度设置为wrap_content但不是高度。

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/fifthRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/forthRow"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/bChangeBase"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/bnb"
            android:onClick="onClickBaseChange" />

        <!--- three more buttons --->            

    </LinearLayout>

    <LinearLayout
        android:id="@+id/firstRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/bP"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/bp" />

        <!--- three more buttons --->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/thirdRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/secondRow"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/b4"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/b4"
            android:onClick="onClickB4" />

        <!--- three more buttons --->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/forthRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/thirdRow"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/b7"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/b7"
            android:onClick="onClickB7" />

        <!--- three more buttons --->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/secondRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/firstRow"
        android:layout_centerHorizontal="true">

        <Button
            android:id="@+id/b1"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/b1"
            android:onClick="onClickB1" />

        <!--- three more buttons --->

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 你考虑过使用 GridLayout 还是表格?

标签: android xml android-layout layout


【解决方案1】:

您有冲突的布局属性。您的firstRow 具有android:layout_alignParentBottom="true",这在与wrap_content 一起使用时会导致问题。每个属性都试图依赖另一个属性来确定视图底部的位置。

此处接受的答案中的更多详细信息:RelativeLayout is taking fullscreen for wrap_content

您是否考虑过使用GridLayout

【讨论】:

    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2021-03-02
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多