簡體   English   中英

具有高度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進行布局嘗試1

底部已被切斷。 問題是ConstraintLayout不能正確調整其高度以嘗試尊重adjustViewBounds上的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>

不幸的是,這產生了:

使用ConstraintLayout進行布局嘗試2

因此,似乎使用layout_constraintHeight_default會破壞ImageView的adjustViewBounds

那么,該怎么辦呢?

注意:我在RecyclerView中使用ConstraintLayout,並且必須使用height 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之間有一些不必要的向后兼容的更改。 請參閱此問題以圍繞庫更改的一種方式進行一些討論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM