Android 在TextView(ConstraintLayout)的顶部添加ImageView

Android 在TextView(ConstraintLayout)的顶部添加ImageView,android,android-constraintlayout,Android,Android Constraintlayout,我没有找到任何方法将带有ConstraintLayout的ImageView置于TextView之上。它应该看起来像下面的屏幕截图,但如果我运行我的应用程序,imageview就会消失 编辑: 将android:translationZ=“2dp”添加到ImageView并不能解决此问题。 但是,以下是我的整个XML文件: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.Constraint

我没有找到任何方法将带有ConstraintLayout的ImageView置于TextView之上。它应该看起来像下面的屏幕截图,但如果我运行我的应用程序,imageview就会消失

编辑:

将android:translationZ=“2dp”添加到ImageView并不能解决此问题。 但是,以下是我的整个XML文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/line1"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorTableWhite"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:textColor="@color/colorTableBlack" />

    <TextView
        android:id="@+id/line2"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorTableWhite"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line1"
        tools:textColor="@color/colorTableBlack" />

    <TextView
        android:id="@+id/line3"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorTableRed"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line2"
        tools:textColor="@color/colorTableWhite" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:translationZ="2dp"
        android:layout_height="60dp"
        android:layout_marginStart="8dp"
        android:foregroundGravity="top"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/space2"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:gravity="left"
        android:text="Lorem ipsum"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/line1"
        app:layout_constraintTop_toBottomOf="@+id/space2" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="36dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Lorem ipsum dolor sit amet, consetetur sadipscing"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/line2"
        app:layout_constraintTop_toBottomOf="@+id/title" />

    <Space
        android:id="@+id/space2"
        android:layout_width="225dp"
        android:layout_height="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.426"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Space
        android:id="@+id/space"
        android:layout_width="225dp"
        android:layout_height="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/description" />

</android.support.constraint.ConstraintLayout>

我找到了两种解决此问题的方法:

  • 您的minSdk=24,因此无需使用srcCompat。尝试更改此选项:
  • app:srcCompat=“@drawable/ic_warning”

    为此:

    android:background=“@drawable/ic_warning”

  • 如果您更喜欢使用app:srcCompat,那么应该对TemplateRow.class中的getView方法进行一些修改。下面是getView()方法的修改版本:


  • 我为这个问题找到了两种解决方案:

  • 您的minSdk=24,因此无需使用srcCompat。尝试更改此选项:
  • app:srcCompat=“@drawable/ic_warning”

    为此:

    android:background=“@drawable/ic_warning”

  • 如果您更喜欢使用app:srcCompat,那么应该对TemplateRow.class中的getView方法进行一些修改。下面是getView()方法的修改版本:


  • 由于您使用的是
    app:srcCompat
    ,因此在XML布局中将
    ImageView
    更改为
    AppCompatImageView

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:translationZ="2dp"
        android:layout_height="60dp"
        android:layout_marginStart="8dp"
        android:foregroundGravity="top"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/space2"
        app:srcCompat="@drawable/ic_launcher_foreground" />
    

    由于您使用的是
    app:srcCompat
    ,因此在XML布局中将
    ImageView
    更改为
    AppCompatImageView

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:translationZ="2dp"
        android:layout_height="60dp"
        android:layout_marginStart="8dp"
        android:foregroundGravity="top"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/space2"
        app:srcCompat="@drawable/ic_launcher_foreground" />
    

    app:layout_constraintTop_toBottomOf=“@+id/space2”,检查space2的位置。你可以发布更多的xml吗?@Aaron是的,我已经发布了app:layout_constraintTop_toBottomOf=“@+id/space2”,检查space2的位置。你可以发布更多的xml吗?@Aaron是的,我已经发布了我的最小API级别是24。不幸的是,这两个都不适合我。我现在添加了完整的xml。我尝试了您的xml,只是更改了颜色,它可以使用或不使用提升。编译dk:28和minSDK:24得到了一个屏幕截图:也许你的代码中有什么东西使你的视图不可见或者改变了它的位置。我不明白这一点。我检查了它,但我从未在代码中对ImageView做过任何操作。我唯一要做的就是把它列在一个清单上。如果你想,你可以看看这里:好的。我现在正在寻找我的最小API级别是24。不幸的是,这两个都不适合我。我现在添加了完整的xml。我尝试了您的xml,只是更改了颜色,它可以使用或不使用提升。编译dk:28和minSDK:24得到了一个屏幕截图:也许你的代码中有什么东西使你的视图不可见或者改变了它的位置。我不明白这一点。我检查了它,但我从未在代码中对ImageView做过任何操作。我唯一要做的就是把它列在一个清单上。如果你想,你可以看看这里:好的。我现在正在寻找好的解决方案@Aaron。比我的更简单更好。很好的解决方案@Aaron。比我的更简单更好。
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:layout_height="60dp"
        android:layout_marginStart="8dp"
        android:foregroundGravity="top"
        android:translationZ="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/space2"
        android:src="@drawable/ic_warning" />