首页 > 解决方案 > 如何在 ConstraintLayout 中的 ImageVew 上放置 TextView

问题描述

我正在使用 ConstraintLayout,我想在 ImageView 上放置一个 TextView。

但我无法在 ConstraintLayout 中找到此功能。

<?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"
tools:context="com.example.sayani.myApp.MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="4dp"
    android:layout_marginEnd="4dp"
    android:layout_marginStart="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@drawable/image1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="240dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>

在上面的代码中,TextView 隐藏在 ImageView 下。
如何将文本放在图像顶部?

标签: androidandroid-studioandroid-constraintlayoutvisibledepth-buffer

解决方案


如果您的应用具有 21 或更高版本的最低 SDK,您可以将其添加到您希望显示在另一个上方的任何 UI 元素中:android:elevation="4dp"

不一定是 4dp,它可以更高或更低,具体取决于您的项目中的工作原理。


推荐阅读