android - Scrollview 和 layout_constraintHeight_max 使 View 消失

标签 android android-layout

我有一个由 ScrollViewConstraintLayout 作为容器组成的布局。这个容器有一些 View ,我希望其中一个 View 具有最大高度,所以我使用了 layout_constrainedHeight_max,但是在启动应用程序时 View 就消失了...

这是我的 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 版。如果 constraintlayout 不在 ScrollView 中,或者如果我不使用 layout_constraintHeight_max(android:maxHeight 不起作用),ImageView1 将出现,但两者的组合不会'不工作。那我错过了什么?

最佳答案

事实证明,要使 layout_constraintWidth_maxlayout_constraintHeight_max 起作用,需要设置所有约束(左、右、上、下)。它不适用于 imageView1 作为顶部具有可变高度的 View 以及与其相关的其他 View 。

我假设您想让 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_constraintHeight="true"app:layout_constraintHeight_max="20dp"

我还建议您将 ConstraintLayoutlayout_height 属性设置为 wrap_content,因为假定 ScrollView 包含一个比自己大的 child 。

关于android - Scrollview 和 layout_constraintHeight_max 使 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54596736/

相关文章:

java - 启动 ACRA 会导致额外的截击问题,可能修复吗?

android - 如何使用 OpenCV 从图像中检测(计数)头发?

android - 定位一张 "list"的图片

java - 如何以编程方式在 Android 的 RelativeLayout 中的按钮旁边显示 TextView ?

java - Viewpager 编辑吗?

java - 将数据从通知发送到已关闭的 Activity

android - 如何在 Android 中平滑地实现放大缩小?与带有 RTMP 流的 SurfaceView 相关

android - 在AlertDialog 框设置透明窗口

android - 焦点在两个 fragment 之间移动

java - 我想在我的 Android 项目中使用 TuProlog