Android 滚动视图和布局\u约束灯光\u最大值使视图消失

Android 滚动视图和布局\u约束灯光\u最大值使视图消失,android,android-layout,Android,Android Layout,我有一个由ScrollView和ConstraintLayout作为容器组成的布局。这个容器有一些视图,我希望其中一个视图有一个最大高度,所以我使用了layout\u constrained height\u max,但是当启动应用程序时,视图就消失了 以下是我的xml示例: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.and

我有一个由
ScrollView
ConstraintLayout
作为容器组成的布局。这个容器有一些视图,我希望其中一个视图有一个最大高度,所以我使用了
layout\u constrained height\u 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一直在消失,我不明白为什么
fillViewport
IsCrollContainer
没有任何更改,我正在使用constraint layout的1.1.3版。如果constraintlayout不在
滚动视图中,或者如果我没有使用
布局\u constraintHeight\u max
(android:maxHeight不起作用),但两者的组合不起作用,则会出现ImageView1。那么我错过了什么

事实证明,对于
layout\u constraintWidth\u max
layout\u constrainthight\u max
,需要设置所有约束(左、右、上、下)。它不适用于图像视图1作为顶部具有可变高度的视图以及与之相关的其他视图

我假设您希望使您的图像视图1的最大高度为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" />

变化如下:

  • 添加
    android:maxHeight=“20dp”
    android:adjustViewBounds=“true”
  • 删除
    app:layout\u constrained height=“true”
    app:layout\u constrained height\u max=“20dp”

  • 我还建议您将
    ConstraintLayout
    layout\u height
    属性设置为
    wrap\u content
    ,因为
    ScrollView
    被假定包含一个比自身大的子对象。

    Hi@Kelly哪一个是ImageView1,因为似乎这两个都是这样命名的?请问下面的图片还是上面的图片正在消失?谢谢。作为测试,你能给第一张图片增加一些宽度和高度,而不是使用包裹内容吗?另外,你的第二张图片应该有那么大,1500dp还是应该只有150dp?它看起来太大了,这可能就是为什么没有显示另一张图片的原因。我说的是最上面的图片,有app:layout_constraintheighth_max的图片,第二张图片只是用来滚动的(事实上,它可以是任何图片或多个视图,但对第一张图片没有影响)。我尝试了固定高度,而不是像你建议的那样包装内容。ImageView1确实出现了,但在我固定的高度,app:layout\u ConstraintheRight\u max变得毫无用处,这不是我所希望的。将ImageView1视为一个事先不知道大小但不想显得太大的图像。如果删除第二个图像,第一个图像仍然会消失?@KellyRuby如果使用约束,请尝试将布局高度设置为
    0dp
    。我完全按照您说的做了,ImageView1确实会出现,但奇怪的是,我无法滚动。对不起,我最初的解决方案不正确。看看我编辑过的答案