2017-10-09 49 views
5

我在NestedScrollView内有ConstraintLayoutConstraintLayout包含一堆视图,但最后的View可以有一个动态高度来填充底部空间(如果有),但如果没有足够的空间,它也需要是最小高度。查看minHeight in ConstraintLayout

为了讨论起见,下面是一个例子。

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

正如你所看到的,我已经把ConstraintLayout版本,但它不起作用。很明显,这些值非常大,但这只是为了测试。

如果我不设置在NestedScrollView然后ConstraintLayoutfillViewport="true"具有0的高度。当我设置fillViewport,该ConstraintLayout不滚动,但只是充满屏幕。

如何设置视图使其扩大到ConstraintLayout的底部,该视口应与视口一样大,但如果我的视图不是minHeight那么我们允许滚动?

我正在使用ConstraintLayout库的1.0.2版本。

我期望看到的是一直到父母的底部,但如果该大小小于1500dp那么视图会滚动。

我输入了1500dp,就像android:layout_height="1500dp"那样,并且视图会相应地滚动。

更新1

好像是有一次我把一个FragmentViewPager内的布局。 app:layout_constraintHeight_min属性不受尊重,它只匹配视口的高度。

我也试着把NestedScrollView从片段中拿出来,把ViewPager放在里面,但是再次没有成功。

+0

您正在使用哪个版本的'ConstraintLayout'? – Cheticamp

回答

6

这个属性添加到视图,您想有弹力:

app:layout_constraintHeight_default="spread" 

我做了一个小的应用程序来证明。没有Java道理可讲的,但这里的布局:

<?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> 

仰视图拉伸以填充视口时,它比剩余的可用空间变小,滚动是不可能的:

enter image description here

当底部视图大于剩余可用空间时,它会保持一个固定的高度,从而使滚动成为可能:

enter image description hereenter image description here

+0

看起来这是正确的答案。 – azizbekian

+0

感谢您的彻底解答。我用你的答案来调试为什么约束布局不起作用,并发现其他具有不同约束的视图并没有很好地与它配合。我改变了这些其他观点受到限制的方式,并开始工作。我的下一个问题是,为什么它不工作,当我把它放在我的ViewPager中... – StuStirling

+0

好吧,它似乎是'ViewPager'这是造成问题 – StuStirling

0

我使用com.android.support.constraint:constraint-layout:1.0.2,这对我的作品:

<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

第一件事是我们必须为每个文本视图或使用包装内容指定固定高度作为另一个选项。内部约束布局的属性app:layout_constraintHeight_default =“spread”帮助最后一个视图获取剩余的剩余空间,如果没有剩余空间,则自动同步滚动视图。

<?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>