Search code examples
androidandroid-constraintlayoutandroid-layout-weight

In ConstraintLayout, what is the meaning of android:layout_width="0dp", when there is no `weight` attribute?


I have this layout:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlayGoogleMaterialIcons">

  <LinearLayout
      android:layout_height="wrap_content"
      android:layout_width="0dp">

...

What is the meaning of android:layout_width="0dp" not in the child of LinearLayout?

What is the meaning of android:layout_width="0dp" when there is no weight attribute?


Solution

  • for the children of ConstraintLayout if you have set constraints then the 0dp is for match_constraint (take full width, or full height)

    Using 0dp, which is the equivalent of "MATCH_CONSTRAINT" https://developer.android.com/reference/android/support/constraint/ConstraintLayout

    example

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/tv_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:text="text1" />
    
        <TextView
            android:id="@+id/tv_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toStartOf="@id/tv_3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_1"
            tools:text="text2" />
    
        <TextView
            android:id="@+id/tv_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/tv_2"
            app:layout_constraintTop_toBottomOf="@id/tv_1"
            tools:text="text3" />
    </android.support.constraint.ConstraintLayout>
    

    enter image description here

    in the above code (and image) you see that text1 TextView width is the width it needs to write the text text1

    for text2 and text3 TextView it takes the whole width divided by 2 that is what the constraints say