ConstraintLayout:将TextView放在ImageView下面

asc*_*sco 7 android android-layout android-constraintlayout

我面临着一个看似简单的任务:布局:

  • 一种ImageView根据图像的宽高比完全占据宽度,高度
  • TextView下面那个

这就是我所拥有的

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/hamster"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText "
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image_view"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

这就是它的样子:

在此输入图像描述

正如你所看到的,它ImageView有一个我不想要的顶部边缘.

如果我改变ImageView's约束

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
Run Code Online (Sandbox Code Playgroud)

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
Run Code Online (Sandbox Code Playgroud)

(删除底部约束),结果如下所示:

在此输入图像描述

现在ImageView它位于我的布局的最顶端,但TextView已经消失了.请注意,对于第二个版本,Android Studio 3的布局预览显示了我想要的布局:

在此输入图像描述

我做错了什么,我该如何解决?

注意:布局需要ConstraintLayout和布局的高度必须一致wrap_content.

你可以在这里找到我的测试项目.

编辑:问题似乎只发生在较大的设备上.我正在使用Nexus 9仿真器.

Dil*_*tel 4

是的,正如 Akash Amin 所说,在您的 ImageView 中添加这两行

    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="0.0"
Run Code Online (Sandbox Code Playgroud)