如何用动态内容约束ScrollView?

时间:2018-11-05 16:48:56

标签: android-fragments android-scrollview android-constraintlayout

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="25dp">
    <TableLayout
        android:id="@+id/tableLayoutHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewDeviceName">
    </TableLayout>

    <ScrollView
        android:id="@+id/scrollViewLayoutFoo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:minWidth="150dp"
        android:minHeight="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tableLayoutHeader">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linearLayoutFragments1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" />


            <LinearLayout
                android:id="@+id/linearLayoutFragments2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"></LinearLayout>
        </LinearLayout>

    </ScrollView>
</android.support.constraint.ConstraintLayout>

linearLayoutFragments1和linearLayoutFragments2在运行时添加了许多片段。不幸的是,scrollViewLayoutFoo并不受限于tableLayoutHeader,但会溢出到tableLayoutHeader中。

任何人都可以提供有关如何解决此问题的提示吗?

1 个答案:

答案 0 :(得分:0)

您缺少一些限制。

您需要从开始/顶部/结尾将TabLayout约束为 parent ,并将视图约束滚动到其底部。

还引用了一些示例中未包含的视图,因此您可能必须相应地更新它们。

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

    <TableLayout
        android:id="@+id/tableLayoutHeader"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_height="48dp" />

    <ScrollView
        android:id="@+id/scrollViewLayoutFoo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:minHeight="60dp"
        android:minWidth="150dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tableLayoutHeader">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/linearLayoutFragments1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />


            <LinearLayout
                android:id="@+id/linearLayoutFragments2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </LinearLayout>

    </ScrollView>
</android.support.constraint.ConstraintLayout>