我有一个由 ScrollView
和 ConstraintLayout
作为容器组成的布局。这个容器有一些 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 一直消失,我不明白为什么。 fillViewport
或 isScrollContainer
没有任何改变,我使用的是约束布局的 1.1.3 版。如果 constraintlayout 不在 ScrollView
中,或者如果我不使用 layout_constraintHeight_max
(android:maxHeight 不起作用),ImageView1 将出现,但两者的组合不会'不工作。那我错过了什么?
最佳答案
事实证明,要使 layout_constraintWidth_max
和 layout_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" />
这里的变化是:
- 添加
android:maxHeight="20dp"
和android:adjustViewBounds="true"
- 删除
app:layout_constraintHeight="true"
和app:layout_constraintHeight_max="20dp"
我还建议您将 ConstraintLayout
的 layout_height
属性设置为 wrap_content
,因为假定 ScrollView
包含一个比自己大的 child 。
关于android - Scrollview 和 layout_constraintHeight_max 使 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54596736/