简体   繁体   中英

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 LinearLayout s 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.

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.

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 with vertical 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.

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

  • Your text-view isn't stretched to full screen , it's set to center by gravity that's why it's look like it is stretched horizontally but not vertically.

  • And you've placed it in linear layout which sets child line vise based accordingly orientation that's why it is not stretching vertically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM