<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没有正确地调整它的高度以尝试在图像上尊重
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>
不幸的是,这会产生:
因此,使用
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之间存在一些不一定向后兼容的更改;请参见this question以了解有关库已更改的一种方式的一些讨论。