scrollview内的Android约束布局高度无效

问题描述 投票:1回答:1

在Android中,我希望在屏幕上实现具有固定高度的滚动视图,并且内部内容也具有固定高度。

滚动视图的高度为300dp,直接子级(相对布局)为500dp,文本视图到顶部的距离为301dp。这意味着我进入文本视图后,还有200dp的底部空间可以从相对布局高度滚动。

enter image description here

我设法使用下面的XML创建所需的效果。

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="300dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:background="#FFC0CB"
            android:layout_height="500dp" >

            <TextView
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:id="@+id/new_realm_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="301dp"
                android:text="long text" />
        </RelativeLayout>
    </ScrollView>

但是这里是问题,如果我将相对布局更改为约束布局,现在滚动将仅滚动到文本视图,高度为310dp,而不是在底部显示200dp的空白。

ConstraintLayout Scroll

有人可以解释为什么约束布局给我这种怪异的行为吗?根据Differences between ConstraintLayout and RelativeLayout,约束布局“既具有相对布局又具有线性布局的双重功效”,它应该能够实现相对布局可以实现的目标。

android xml scrollview android-constraintlayout android-relativelayout
1个回答
0
投票

尝试一下:

<ScrollView android:layout_width="match_parent"
android:layout_height="300dp">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:background="#FFC0CB"
    android:minHeight="500dp"
    android:layout_height="500dp" >

    <TextView
        android:id="@+id/new_realm_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="301dp"
        android:text="long text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

似乎约束布局中存在错误,或者布局高度无法应用于滚动视图中的约束布局,但是您可以在约束布局中使用最小高度属性。

© www.soinside.com 2019 - 2024. All rights reserved.