【问题标题】:LinearLayout doesn't show when I put it into a ConstraintLayout当我将 LinearLayout 放入 ConstraintLayout 时不显示
【发布时间】:2020-12-20 23:10:02
【问题描述】:

我有一个带有不同视图的 LinearLayout,它在我的手机中安装时可以正常显示。但是我想将该 LinearLayout 放入一个 ContraintLayout,但是当我这样做时,我的屏幕上什么都没有显示。

这段代码运行良好

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

        <LinearLayout
            android:id="@+id/bottomMenu"
            android:layout_width="match_parent"
            android:layout_height="@dimen/short_cuts_bottom_menu_layout_height"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

                <!-- Views -->

        </LinearLayout>


</layout>

但这不起作用

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/bottomMenu"
            android:layout_width="match_parent"
            android:layout_height="@dimen/short_cuts_bottom_menu_layout_height"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

                <!-- Views -->

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

我只是将我的 LinearLayout 放入 ConstraintLayout

有人知道为什么会这样吗?

【问题讨论】:

    标签: android android-linearlayout android-constraintlayout


    【解决方案1】:

    ConstraintLayout 中的android:layout_width 属性应该具有match_parent 的值,因为其中的LinearLayout 具有相同的值

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    

    【讨论】:

      【解决方案2】:

      通过将 wrap_content 分配给您的 ConstraintLayout 的宽度,您可以告诉它根据其子项的尺寸来定义其尺寸。通过将 match_parent 分配给 LinearLayout 的宽度,您可以告诉它根据其父级定义其尺寸。所以他们一直在问对方,没人知道:)

      您可以简单地将 match_parent 分配给约束布局的宽度

      【讨论】:

        猜你喜欢
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-26
        • 1970-01-01
        相关资源
        最近更新 更多