ConstraintLayout with height wrap_content does not

2019-07-15 23:58发布

I am trying to create this:

Layout with Linear Layout

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>

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>

Unfortunately this produces:

Layout attempt 1 with ConstraintLayout

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>

Unfortunately this produces:

Layout attempt 2 with ConstraintLayout

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

So, how can this be done?

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

1条回答
Fickle 薄情
2楼-- · 2019-07-16 00:44

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'

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.

查看更多
登录 后发表回答