高度 wrap_content 的 ConstraintLayout 不适用于 ImageView adjustViewBounds

ConstraintLayout with height wrap_content does not work with ImageView adjustViewBounds

我正在尝试创建这个:

并且可以使用此 LinearLayout:

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <ImageView android:id="@+id/image"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="48sp"
        android:text="Hello World" />

</LinearLayout>
</FrameLayout>

效果很好。现在,我正在尝试对 ConstraintLayout 做同样的事情:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout 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="wrap_content"
    android:background="@android:color/white">

    <ImageView android:id="@+id/image"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="48sp"
        android:text="Hello World"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image" />

</android.support.constraint.ConstraintLayout>
</FrameLayout>

不幸的是,这会产生:

底部被切掉了。问题是 ConstraintLayout 没有正确调整其高度以尝试在图像上尊重 adjustViewBounds。我看到了使用 layout_constraintHeight_default 的建议并尝试过:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout 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="wrap_content"
    android:background="@android:color/white">

    <ImageView android:id="@+id/image"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="48sp"
        android:text="Hello World"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image" />

</android.support.constraint.ConstraintLayout>
</FrameLayout>

不幸的是,这会产生:

所以,layout_constraintHeight_default 的使用似乎破坏了 ImageView 的 adjustViewBounds

那么,如何做到这一点?

注意:我在 RecyclerView 中使用 ConstraintLayout,必须使用高度 wrap_content

我试图自己重现你的问题。我发现当我使用 constraint-layout 库的 1.0.2 版时我遇到了与您看到的相同的问题,但是当我使用 1.1.0-beta4 版时我没有。您可以将 gradle 文件中的依赖项更改为

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'

请注意,1.0.2 和 1.1.0-beta4 之间有一些不一定向后兼容的更改;请参阅 以了解有关库更改的一种方式的一些讨论。