【发布时间】:2018-01-11 08:51:18
【问题描述】:
我正在尝试创建这个:
并且可以使用此 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 没有正确调整其高度以尝试尊重图像上的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库?
标签: android android-constraintlayout