View from minHeight in ConstraintLayout

I have a ConstraintLayout inside a NestedScrollView . ConstraintLayout contains a bunch of views, but the last View can have a dynamic height to fill the bottom space if there is one, but it should also be a minimum height if there is not enough space.

For arguments, here is an example.

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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" tools:layout_height="match_parent"> <View android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_min="1500dp" android:background="@color/red" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> </android.support.v4.widget.NestedScrollView> 

As you can see, I have included the ConstraintLayout version, but it does not work. Obviously, the values โ€‹โ€‹are ridiculously large, but it's just for testing.

If I do not set fillViewport="true" in NestedScrollView , then ConstraintLayout has a height of 0. When I set fillViewport , ConstraintLayout does not scroll, it just fills the screen.

How can I set the view so that it extends to the bottom of the ConstraintLayout , which should be the size of the Viewport, but if my view is not minHeight , then we allow scrolling?

I am using version 1.0.2 the ConstraintLayout library.

What I expect to see is to be up to the end of the parent, but if this size is less than 1500dp , then the view scrolls.

I entered 1500dp, like android:layout_height="1500dp" , and the view scrolls accordingly.

UPDATE 1

It seems I once posted a layout in a FragmentViewPager . The app:layout_constraintHeight_min not respected and only matches the height of the viewport.

I also tried pulling the NestedScrollView from the fragment and pasting the ViewPager inside it, but was not working again.

+5
source share
3 answers

Add this attribute to the view you want to stretch:

 app:layout_constraintHeight_default="spread" 

I made a small application to demonstrate. No Java logic to say, but here is 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="#ddd" 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 is stretched to fill the viewport when it is less than the remaining free space, and scrolling is impossible:

enter image description here

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

enter image description here enter image description here

+6
source

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
source

First of all, we must specify a fixed height for each text view or use the contents of the wrapper as another parameter. The second external app property restriction template : layout_constraintHeight_default = "spread" helps the latter to get the remaining full space to the left and, if there is no free space, it will automatically synchronize to view the scroll.

  <?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="#ddd 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
source

Source: https://habr.com/ru/post/1272463/


All Articles