Search code examples
androidandroid-linearlayoutandroid-xml

Linear Layout - fixed height children are wrongly placed


I'm placing 3 TextView in a LinearLayout and want them to be equaly distributed AND to have a fixed height. The combination of layout_weight and a fixed layout_height should make sure, that all 3 views are always shown in one line and are always of equal height. But they aren't.

Can someone explain where the problem lies? I can't find it...

Here's my LinearLayout with it's children:

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

    <TextView
        android:id="@+id/tvMuted"
        android:text="@string/color_palette_muted"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedLight"
        android:text="@string/color_palette_muted_light"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedDark"
        android:text="@string/color_palette_muted_dark"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

</LinearLayout>

And this is what I get:

enter image description here


Solution

  • Just set android:layout_gravity="center" in all the TextView's as follows:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/tvMuted"
            android:text="@string/color_palette_muted"
            android:foreground="?selectableItemBackground"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="4dp"
            android:layout_height="64dp">
        </TextView>
    
        <TextView
            android:id="@+id/tvMutedLight"
            android:text="@string/color_palette_muted_light"
            android:foreground="?selectableItemBackground"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="4dp"
            android:layout_height="64dp">
        </TextView>
    
        <TextView
            android:id="@+id/tvMutedDark"
            android:text="@string/color_palette_muted_dark"
            android:foreground="?selectableItemBackground"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="4dp"
            android:layout_height="64dp">
        </TextView>
    
    </LinearLayout>