user1785730
user1785730

Reputation: 3535

Nested LinearLayout stretch

Consider this layout:

<?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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/red"
                android:background="@color/Red"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Red" />

        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

The TextView is stretched horizontally all the way to the edges of the screen, but the height is limited by the string "Red" and is positioned at the top of the screen. Why is that so? Why is the TextView stretched in only one dimension?

When I switch the orientation of the LinearLayouts the effect is reversed: The TextView is stretched from top to bottom, but its width is limited by the string "Red" and is aligned to the left of the screen.

Upvotes: 0

Views: 170

Answers (3)

ankuranurag2
ankuranurag2

Reputation: 2441

That's how the Android weight distribution is supposed to work. If a view has weight in a LinearLayout with horizontal orientation, then it will acquire importance/weight in horizontal direction only. Similar is the case with LinearLayout withvertical orientation, the view gains importance/weight in the vertical direction. That's how the android weight distribution works.

Note: Use width(in case of horizontal orientation) or height(in case of vertical orientation) as 0dp instead of 'wrap_content' for better performance.

Upvotes: 1

Sejpal Pavan
Sejpal Pavan

Reputation: 140

Do Like This

<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"
tools:context=".MainActivity">

       <TextView
            android:id="@+id/red"
            android:background="@color/Red"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:text="Red" />

Upvotes: 0

Viraj Patel
Viraj Patel

Reputation: 2393

Remove android:layout_weight="1" from TextView and check.

Note: If you are using ConstraintLayout then no need to take LinearLayout. Everything can be manage by ConstraintLayout. You just have to set proper constraints of Views.

Upvotes: 2

Related Questions