【问题标题】:Scrollview inside constraint layout does not scroll to the bottom of the parent constraint约束布局内的滚动视图不会滚动到父约束的底部
【发布时间】:2017-02-25 07:43:48
【问题描述】:

我有一个包含大约 12/13 个字段的表单。我在约束布局中使用了Scrollview。下面是 XML 布局的层次结构。问题是,它不会滚动到底部,而是仅滚动到最初的 10 个视图。由于视图不再滚动,最后 3 个字段被隐藏。

父布局

<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:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical">

<!-- Textview and a button -->

  <ScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical"
    android:overScrollMode="never"
    android:scrollbars="none"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">


  <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

                <!-- Child Views (12/13 views of the fields)-->

  </android.support.constraint.ConstraintLayout>

</ScrollView>

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 尝试将ScrollViewandroid:layout_height参数改为match_parent
  • 试过了,还是不行。
  • 为你的滚动视图设置高度以匹配父级和fillViewPort true,或者尝试嵌套的滚动视图
  • 您使用的是哪个版本的 ConstraintLayout?你能发布完整的 xml 布局吗?
  • 编译'com.android.support.constraint:constraint-layout:1.0.0'

标签: android android-layout constraints scrollview android-constraintlayout


【解决方案1】:

此布局适用于我的应用程序。 诀窍是在 ScrollView 中设置这两个属性: 安卓:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent"

我的应用中的简化布局:

<?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"
    android:theme="@style/ThemeOverlay.AppCompat.Light">

    <RelativeLayout
        android:id="@+id/linear"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:background="@color/title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/linear">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/titleView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:text="@string/title"
                android:textSize="14sp"
                app:layout_constraintBaseline_toBaselineOf="@+id/title"
                app:layout_constraintLeft_toLeftOf="parent" />

            <EditText
                android:id="@+id/title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:hint="toilet title"
                android:inputType="text"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="@+id/open_hour"
                app:layout_constraintLeft_toRightOf="@+id/titleView"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            ...
            Other Views in ScrollView
            ...
        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 这行得通,但是为什么 0dp 可以代替 wrap_content 或 match_parent 起作用?
  • 在我的情况下 app:layout_constraintBottom_toBottomOf="parent" 添加到 ScrollView 作品中。伟大的 。我google了很多次,直到找到这个解决方案。谢谢。
  • 请记住,OUTER contraintlayout 通常应该有android:layout_width="match_parent"android:layout_height="match_parent"。否则使用android:layout_height="0dp" 将看不到滚动视图
【解决方案2】:

在我的情况下,NestedScrollView 代替了ScrollView。以下是我的工作布局的 sn-p:

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

    <!-- Some Views Here -->

    <android.support.v4.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!-- Some Views That can be Scrolled Here -->

        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

【讨论】:

  • 还在Manifest文件Activity中添加android:windowSoftInputMode="stateAlwaysHidden|adjustResize"。
【解决方案3】:

对于这个问题,您有两种解决方案(相同的解决方案,但有两种方法):

  1. 如果您将 Design mode 放在 Android Studio 中,请选择您的 ScrollView 并打开属性选项卡,然后在 layout_height 中选择“ma​​tch_constraint”。

  2. 如果您在 Android Studio 中使用 文本模式,请使用:

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tb_details">
    

看到 ScrollView 的高度设置为 0dp。这两种方法都解决了同一个问题,但它们是不同的方法。

ScrollView 不是根视图,我有一个像你一样包裹 ScrollView 的约束布局。

【讨论】:

  • 这行得通。我有一个带有子 ScrollView 的 ConstraintLayout,它有一个子 LinearLayout,我用行动态填充。此解决方案的关键是高度(滚动轴)必须设置为 0 并且 ScrollView 必须在滚动轴的两侧受到约束(在这种情况下为顶部和底部约束。如果您有水平滚动视图您需要将宽度设置为 0 并设置开始和结束约束)
【解决方案4】:

两步

  1. 保持滚动视图的布局高度为零
    android:layout_height="0dp"

  2. 再次滚动查看
    android:fillViewport="true"

【讨论】:

  • 我的情况是高度和宽度都需要为 0dp,滚动边需要为 match_parent,否则滚动不起作用。当然,所有 4 个约束都应该添加到 RecyclerView
【解决方案5】:

尝试将底部约束添加到滚动视图(例如:app:layout_constraintBottom_toBottomOf="parent") 并将 android:layout_height="wrap_content" 更改为 android:layout_height="0dp"

【讨论】:

  • 我忘记了滚动视图的底部约束。非常感谢:D
【解决方案6】:

在我的例子中,NestedScrollView 代替了ScrollView

以下是我的工作布局的 sn-p: 请确保您没有为滚动视图 android:fillViewport="true;

设置任何子视图高度以匹配 constrianlayout 内的父级(0 dp)

如果有任何疑问,请询问我。

<android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/_90sdp"
        android:fillViewport="true">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/cvLayout"
            android:animateLayoutChanges="true">

【讨论】:

  • 在我的情况下 ScrollView 不工作,但 NestedScrollView 工作正常..
【解决方案7】:

只需将android:fillViewport="true" 放在父布局中

【讨论】:

    【解决方案8】:

    在我的例子中,我在 ScrollView(高度设置为 0dp 并在顶部和底部受到约束)中有一个高 TextView(高度设置为 wrap_content)。没有任何建议有效,但我通过将 TextView 包裹在 FrameLayout 内(高度设置为 wrap_content)解决了这个问题。

    【讨论】:

      【解决方案9】:

      如果 ConstraintLayout 位于 SV/NestedSV 内部,则永远不要保持 0dp 子级高度

      wrap_content 有效,因为在这种情况下,ScrollView 知道它的子项高度。

      【讨论】:

      • 这个!!!!永远不要让约束布局 0DP!!!!谢谢你
      【解决方案10】:

      对我来说,我需要在我的 ScrollView 中添加一个 LinearLayout 来约束它

      <androidx.constraintlayout.widget.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">
      
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toTopOf="parent"
              tools:layout_editor_absoluteX="1dp">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
      
                  <View....</View>
      
              </LinearLayout>
      
          </ScrollView>
      
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        【解决方案11】:

        我已经解决了这个问题。在 ScrollView 内你不能使用约束布局。 为了在滚动中使用约束,你必须使用相对布局是约束布局的父级..

        所以你的顺序应该是:

        ScrollView ---> 相对布局 ---> 约束布局

        【讨论】:

          【解决方案12】:

          如果您在搜索“软键盘隐藏视图元素”后来到这里! 然后你只需要再次添加一个滚动视图和相同的布局元素。
          之前

          <androidx.constraintlayout.widget.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">
          
             <ImageView
                 android:id="@+id/imageView3"
                 android:layout_width="246dp"
                 android:layout_height="168dp"
                 android:layout_marginTop="20dp"
                 android:src="@drawable/img"/>
             <Button
                 android:id="@+id/btn"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"/>
          </androidx.constraintlayout.widget.ConstraintLayout>
          


          之后

          <androidx.constraintlayout.widget.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">
          
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="0dp"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintBottom_toBottomOf="parent"
              android:fillViewport="true">
          
                  <androidx.constraintlayout.widget.ConstraintLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
          
                      <ImageView
                           android:id="@+id/imageView3"
                           android:layout_width="246dp"
                           android:layout_height="168dp"
                           android:layout_marginTop="20dp"
                           android:src="@drawable/img"/>
                      <Button
                           android:id="@+id/btn"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"/>
                  </androidx.constraintlayout.widget.ConstraintLayout>
           </ScrollView>
          </androidx.constraintlayout.widget.ConstraintLayout>
          

          【讨论】:

            【解决方案13】:

            对于水平滚动视图的情况(父级是ConstraintLayout,直接子级是LinearLayout),我发现设置四个约束,layout_width=0dp,fillViewport=true是不够的。它仍然没有滚动。

            在我的案例中起作用的是设置四个约束并将元素从 ScrollView 更改为 Horizo​​ntalScrollView。在这种情况下,layout_width 可以设置为“wrap_content”并且可以省略 fillViewport。 此外,我在 Horizo​​ntalScrollView 的直接子项的末尾添加了一个填充,以使滚动体验和外观更好。

            【讨论】:

              【解决方案14】:

              我遇到了另一个问题,我有 nestedscrollviewconstrainlayoutlinearlayout。这个linearlayout 以编程方式添加了孩子。所以滚动不起作用。由replacingCL 解决,LL 垂直方向

              <androidx.core.widget.NestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  app:layout_constraintTop_toBottomOf="@+id/separatr"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintBottom_toBottomOf="parent"
                  android:fillViewport="true"
                  android:fitsSystemWindows="true"
                  >
              
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:fitsSystemWindows="true"
                      android:orientation="vertical"
                      tools:background="@color/yellow_highlight"
                      android:paddingBottom="@dimen/box96">
              
              
                      <TextView
                          android:id="@+id/qansTv"
                          style="@style/BodyText"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_marginStart="@dimen/box200"
                          android:layout_marginTop="@dimen/box32"
                          android:layout_marginEnd="@dimen/box200"
                          android:text="Lorem ipsum dofdfd fsd fdfsd sdf sdfsd fsdfsd fd sdfd fsdfsdf sdfsd df sdfd fsdfsd fsdf sdfsd dflors fdfdf."
                          android:textColor="@color/white"
                          app:layout_constraintEnd_toEndOf="parent"
                          app:layout_constraintStart_toStartOf="parent"
                          app:layout_constraintTop_toTopOf="parent" />
              
                      <LinearLayout
                          android:id="@+id/ansImageContainerLL"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          tools:background="@color/red"
                          android:layout_marginEnd="@dimen/box64"
                          android:orientation="vertical"
                          android:layout_marginStart="@dimen/box64"
                          app:layout_constraintEnd_toEndOf="parent"
                          app:layout_constraintBottom_toBottomOf="parent"
                          app:layout_constraintStart_toStartOf="parent"
                          app:layout_constraintTop_toBottomOf="@+id/qansTv" />
              
                  </LinearLayout>
              </androidx.core.widget.NestedScrollView>
              

              【讨论】:

                【解决方案15】:

                在我的情况下,我必须使用 2 个 recyclerview,因为它不想一直滚动,所以我找到了一种方法来设置滚动视图的滚动长度,长度是通过更改 TextView 的宽度来设置的.

                <?xml version="1.0" encoding="utf-8"?>
                <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/activity_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".ui.ProjectsFragment"
                android:background="@color/blue">
                
                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/scrollViewLength"
                        android:layout_width="2000dp"
                        android:layout_height="match_parent"
                        android:text="Change the width above"
                        android:visibility="invisible"/>
                
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:tag="exampletag"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView1"
                        android:tag="exampletag"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginTop="28dp"
                
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        />
                </FrameLayout>
                
                </HorizontalScrollView>
                

                【讨论】:

                  【解决方案16】:

                  我知道我迟到了一点,我遇到了同样的问题,就像我将 RecyclerView 包裹在线性或相对布局中一样,它就像一个魅力,

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-10-09
                    • 2021-06-10
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-01-05
                    • 1970-01-01
                    相关资源
                    最近更新 更多