View with minHeight in ConstraintLayout

后端 未结 3 1330
天命终不由人
天命终不由人 2020-12-30 03:46

I have a ConstraintLayout inside a NestedScrollView. The ConstraintLayout contains a bunch of views but the last View can

相关标签:
3条回答
  • 2020-12-30 04:10

    Add this attribute to the view you'd like to have stretch:

    app:layout_constraintHeight_default="spread"
    

    I made a tiny app to demonstrate. No java logic to speak of, but here's the layout:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:background="#caf">
    
            <TextView
                android:id="@+id/one"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:gravity="center"
                android:text="hello world"
                android:background="#fff"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/two"/>
    
            <TextView
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:gravity="center"
                android:text="hello world"
                android:background="#eee"
                app:layout_constraintTop_toBottomOf="@+id/one"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/three"/>
    
            <TextView
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:gravity="center"
                android:text="hello world"
                android:background="#ffffd"
                app:layout_constraintHeight_default="spread"
                app:layout_constraintHeight_min="300dp"
                app:layout_constraintTop_toBottomOf="@+id/two"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
    
        </android.support.constraint.ConstraintLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    The bottom view stretches to fill the viewport when it is smaller than the remaining available space, and scrolling is impossible:

    The bottom view maintains a fixed height when it is larger than the remaining available space, which makes scrolling possible:

    0 讨论(0)
  • 2020-12-30 04:13

    I am using com.android.support.constraint:constraint-layout:1.0.2 and this works for me:

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@drawable/gradient"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_min="1500dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </android.support.constraint.ConstraintLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
  • 2020-12-30 04:26

    As the first thing is we have to specify the fixed height to each text view or used wrap content as another option.Secondary inside constraint layout the property app:layout_constraintHeight_default="spread" helps the last view to get the remaining complete space left and if no space left then automatically synced to scroll view.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#caf"
            android:padding="16dp">
    
            <TextView
                android:id="@+id/one"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:background="#fff"
                android:gravity="center"
                android:text="hello world"
                app:layout_constraintBottom_toTopOf="@+id/two"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:background="#eee"
                android:gravity="center"
                android:text="hello world"
                app:layout_constraintBottom_toTopOf="@+id/three"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/one" />
    
            <TextView
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:background="#eee"
                android:gravity="center"
                android:text="hello world"
                app:layout_constraintBottom_toTopOf="@+id/four"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/two" />
    
            <TextView
                android:id="@+id/four"
                android:layout_width="0dp"
                anroid:layout_height="48dp"
                android:background="#eee"
                android:gravity="center"
                android:text="hello world"
                app:layout_constraintBottom_toTopOf="@+id/five"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/three" />
    
            <TextView
                android:id="@+id/five"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="#ffffd"
                android:gravity="center"
                android:text="hello world"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHeight_default="spread"
                app:layout_constraintHeight_min="300dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/three" />
    
        </android.support.constraint.ConstraintLayout>
    
    </android.support.v4.widget.NestedScrollView>
    
    0 讨论(0)
提交回复
热议问题