简体   繁体   English

具有高度wrap_content的ConstraintLayout不适用于ImageView AdjustViewBounds

[英]ConstraintLayout with height wrap_content does not work with ImageView adjustViewBounds

I am trying to create this: 我正在尝试创建这个:

具有线性布局的布局

And can do so with this LinearLayout: 并可以使用以下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: 现在,我试图对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: 不幸的是,这产生了:

使用ConstraintLayout进行布局尝试1

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. 问题是ConstraintLayout不能正确调整其高度以尝试尊重adjustViewBounds上的adjustViewBounds I have seen suggestions to use layout_constraintHeight_default and have tried that: 我已经看到使用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>

Unfortunately this produces: 不幸的是,这产生了:

使用ConstraintLayout进行布局尝试2

So, it seems use of layout_constraintHeight_default breaks the ImageView's adjustViewBounds . 因此,似乎使用layout_constraintHeight_default会破坏ImageView的adjustViewBounds

So, how can this be done? 那么,该怎么办呢?

NOTE: I am using ConstraintLayout inside a RecyclerView and must use height wrap_content for it. 注意:我在RecyclerView中使用ConstraintLayout,并且必须使用height wrap_content

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. 我发现,当我使用constraint-layout库的1.0.2版本时,遇到的问题与您看到的相同,但是当我使用1.1.0-beta4版本时,却没有。 You can change the dependency in your gradle file to 您可以将gradle文件中的依赖项更改为

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; 请注意,在1.0.2和1.1.0-beta4之间有一些不必要的向后兼容的更改。 see this question for some discussion around one way in which the library has changed. 请参阅此问题以围绕库更改的一种方式进行一些讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 marginTop 不适用于 ConstraintLayout 和 wrap_content - marginTop does not work with ConstraintLayout and wrap_content 在Android imageView wrap_content 不起作用 - In Android imageView wrap_content does not work scaleType 和 wrap_content 如何确定 ImageView 的宽度和高度? - How does scaleType and wrap_content determine ImageView width and height? imageview height作为所示图像的wrap_content - imageview height as the wrap_content of the shown image 使用wrap_content高度在FrameLayout中裁剪ImageView - Crop ImageView in FrameLayout with wrap_content height ConstraintLayout 的占位符与 wrap_content 宽度/高度具有奇怪的行为 - ConstraintLayout's Placeholder with wrap_content width/height with an odd behaviour ConstraintLayout不使用RecyclerView中的Wrap_Content动态更改高度 - ConstraintLayout not dynamically changing Height with Wrap_Content inside RecyclerView ConstraintLayout中的FrameLayout wrap_content - FrameLayout wrap_content in ConstraintLayout ImageView:adjustViewBounds不能与layout_height =“fill_parent”一起使用? - ImageView: adjustViewBounds does not work with layout_height=“fill_parent”? Android ConstraintLayout wrap_content不显示所有内容 - Android ConstraintLayout wrap_content does not show all the contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM