【问题标题】:ConstraintLayout with height wrap_content does not work with ImageView adjustViewBounds具有高度 wrap_content 的 ConstraintLayout 不适用于 ImageView adjustViewBounds
【发布时间】:2018-01-11 08:51:18
【问题描述】:

我正在尝试创建这个:

并且可以使用此 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,并且必须使用 height wrap_content

【问题讨论】:

  • 您使用的是哪个版本的constraint-layout 库?

标签: android android-constraintlayout


【解决方案1】:

我试图自己重现您的问题。我发现当我使用 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 之间存在一些不必要的向后兼容更改;请参阅this question,了解有关库更改的一种方式的一些讨论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2020-06-30
    • 2019-03-05
    • 2021-07-07
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多