julio segura
julio segura

Reputation: 63

LinearLayout doesn't show when I put it into a ConstraintLayout

I have a LinearLayout with deferents views, and it displays normally when install it in my phone. But I want to put that LinearLayout into a ContraintLayout, however when I did it, nothing is display in my screen.

This code works well

<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>

But this doesn't works

<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>

And I just put my LinearLayout into a ConstraintLayout

Someone knows why this happens?

Upvotes: 0

Views: 424

Answers (2)

Oya Canli
Oya Canli

Reputation: 2516

By assigning wrap_content to the width of your ConstraintLayout, you tell it to define its dimension based on the dimensions of its children. And by assigning match_parent to the width of your LinearLayout you tell it to define its dimension based on its parent. So they keep asking each other, nobody knows :)

You can simply assign match_parent to the width of your constraint layout

Upvotes: 1

Xid
Xid

Reputation: 4951

The android:layout_width attribute in ConstraintLayout should have the value match_parent because the LinearLayout inside it has the same

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

Upvotes: 1

Related Questions