ontent的ConstraintLayout不适用于Image

ontent的ConstraintLayout不适用于Image

本文介绍了具有高度wrap_content的ConstraintLayout不适用于ImageView AdjustViewBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建它:

并且可以通过以下LinearLayout做到这一点:

And can do so with this 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做同样的事情:

Which works great. Now, I'm trying to do the same with 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的建议并尝试过:

The bottom has been cut off. The issue is the ConstraintLayout is not sizing it's height correctly to try respect adjustViewBounds on the image. I have seen suggestions to use layout_constraintHeight_default and have tried that:

<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.

So, it seems use of layout_constraintHeight_default breaks the ImageView's adjustViewBounds.

那么,该怎么办呢?

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

NOTE: I am using ConstraintLayout inside a RecyclerView and must use height wrap_content for it.

推荐答案

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

I tried to reproduce your problem myself. I found that when I used version 1.0.2 of the constraint-layout library I had the same issue as you see, but when I used version 1.1.0-beta4 I did not. You can change the dependency in your gradle file to

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

请注意,在1.0.2和1.1.0-beta4之间存在一些不必要的向后兼容更改;请参阅此问题,以了解有关库更改的一种方式的一些讨论.

Note that there are some non-necessarily-backwards-compatible changes between 1.0.2 and 1.1.0-beta4; see this question for some discussion around one way in which the library has changed.

这篇关于具有高度wrap_content的ConstraintLayout不适用于ImageView AdjustViewBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:33