ScrollView和layout_constraintHeight_max会导致视图消失

3

我有一个布局,由一个ScrollView和一个ConstraintLayout作为容器组成。这个容器包含一些视图,我希望其中的一个视图有最大高度,因此我使用了layout_constrainedHeight_max,但是当启动应用程序时,该视图就消失了...

这是我的xml示例:

<ScrollView
    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="match_parent"
        android:isScrollContainer="true">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constrainedHeight="true"
            app:layout_constraintHeight_max="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="1500dp"
            android:layout_margin="8dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView1"/>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

我简化了XML以重现最小组件的错误。我尝试了很多方法,但是imageView1一直消失,我不知道为什么。 fillViewportisScrollContainer都没有改变任何东西,我正在使用版本1.1.3的constraint-layout。如果constraintlayout不在ScrollView中,或者我不使用layout_constraintHeight_max(android:maxHeight无效),imageView1将出现,但两者的组合就是不起作用。那么我错过了什么?

嗨@Kelly,请问ImageView1是哪一个,因为似乎两个都是这样被调用的?请问是底部的图片还是顶部的图片消失了?谢谢。 - Francislainy Campos
你可以作为一个测试,给第一张图片添加一些宽度和高度,而不是使用wrap content吗?另外,你的第二张图片应该这么大吗,1500dp,还是只需要150dp?它看起来太大了,这可能是为什么其他图片没有显示出来的原因。 - Francislainy Campos
如果你删除第二张图片,第一张图片仍然消失吗? - Francislainy Campos
第一张图片确实没有出现。事实上,只要第一张图片下面的任何内容不足以滚动,第一张图片就会出现。因此,在这种情况下,滚动视图变得无用,这就是我开始怀疑滚动视图和constraintHeight_max是否兼容的原因。 - Kelly Rudy
1
@KellyRudy 如果你正在使用约束,请将布局高度设置为“0dp”。 - karan
显示剩余2条评论
1个回答

1
事实证明,layout_constraintWidth_maxlayout_constraintHeight_max需要设置所有约束条件(左、右、上、下)才能起作用。这不适用于imageView1,因为它是一个具有可变高度的顶部视图,与其他相关视图不同。
我猜你想让你的imageView1最大高度为20dp。要实现这个目标,请尝试使用以下代码:
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:adjustViewBounds="true"
    android:maxHeight="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher" />

这里的更改如下:
  1. 添加 android:maxHeight="20dp"android:adjustViewBounds="true"
  2. 删除 app:layout_constrainedHeight="true"app:layout_constraintHeight_max="20dp"
我还建议将 ConstraintLayoutlayout_height 属性设置为 wrap_content,因为假定 ScrollView 包含一个比它自己大的子元素。

我按照你说的做了,imageView1确实出现了,但奇怪的是我无法滚动。 - Kelly Rudy
抱歉,我的初始解决方案不正确。请看我的编辑答案。 - Marat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接