约束布局内的Scrollview不会滚动到父约束的底部

Mah*_*ddy 28 android constraints scrollview android-layout android-constraintlayout

我的表格大约有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>
Run Code Online (Sandbox Code Playgroud)

pin*_* li 51

此布局适用于我的应用.诀窍是在ScrollView中设置这两个属性:android: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>
Run Code Online (Sandbox Code Playgroud)

  • 这是可行的,但是为什么 0dp 可以代替 wrap_content 或 match_parent 起作用呢? (2认同)
  • 就我而言 **`app:layout_constraintBottom_toBottomOf="parent" `** 添加到 ScrollView 有效。伟大的 。我用谷歌搜索了很多次,直到找到这个解决方案。谢谢。 (2认同)
  • 请记住,外部 **contraintlayout** 通常应具有 `android:layout_width="match_parent"`、`android:layout_height="match_parent"`。否则,滚动视图将不会通过 `android:layout_height="0dp"` 可见 (2认同)

小智 8

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


Fir*_*mon 8

就我而言,它NestedScrollView代替了ScrollView。以下是我的工作布局摘要:

<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>
Run Code Online (Sandbox Code Playgroud)


Har*_*dar 7

两个步骤

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

  2. 再次滚动视图
    android:fillViewport="true"


ric*_*hah 7

在我的情况下NestedScrollView工作而不是ScrollView.

以下是我的工作布局的片段:请确保您没有使任何子视图高度与 constrianlayout 中的 parent(0 dp) 匹配,也适用于滚动视图android:fillViewport="true ;

问我是否有任何疑问。

<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">
Run Code Online (Sandbox Code Playgroud)


Ser*_*gio 6

您有两个解决方案(相同的解决方案,但是有两种解决方法):

  1. 如果将“ 设计”模式置于Android Studio中,请选择ScrollView并打开“属性”选项卡,然后在layout_height中选择“ match_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">
    
    Run Code Online (Sandbox Code Playgroud)

看到ScrollView的高度设置为0dp。两种方法都可以解决相同的问题,但是解决这些问题的方法不同。

ScrollView不是根视图,我有一个Constraint布局,可以像您一样包装ScrollView。

  • 这有效。我有一个带有子 ScrollView 的 ConstraintLayout,它有一个子 LinearLayout,我动态地填充行。此解决方案的关键是高度(滚动轴)必须设置为 0 并且 ScrollView 必须约束在滚动轴的两侧(在本例中为顶部和底部约束。如果您有水平滚动视图您需要将宽度设置为 0 并设置开始和结束约束) (2认同)