[英]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: 不幸的是,这产生了:
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: 不幸的是,这产生了:
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.