android - 在 ImageView 上添加 TextView (Android)

标签 android android-layout android-imageview textview

我在 ImageView 中显示全屏图像,我试图在 ImageView 的屏幕中心添加 TextView,但 TextView 没有显示。

我试过:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/splash_imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dip"
        android:background="#AA000000"
        android:padding="12dip"
        android:text="SomeText"
        android:textColor="#ffffffff" />

</LinearLayout>

最佳答案

您可以使用 FrameLayoutAbsoluteLayout(已弃用)或 RelativeLayout(其中最常见)。 RelativeLayout 的示例,用于居中 android:layout_centerInParent="true",为两个 child 设置

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/splash_imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dip"
        android:layout_centerInParent="true"
        android:background="#AA000000"
        android:padding="12dip"
        android:text="SomeText"
        android:textColor="#ffffffff" />

</RelativeLayout>

绘制 View 的顺序与在 XML 中的顺序相同,因此将首先绘制 ImageView,然后在其上绘制 TextView

另一种方法是使用 FrameLayoutandroid:gravity="center" attr( child 不需要)

还要记住并阅读有关 android:elevationandroid:translationZ 的 XML 属性,它们可能会重新排序绘图,还有 view.bringToFront() 方法

关于android - 在 ImageView 上添加 TextView (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425026/

相关文章:

Android - 在更新 SDK 版本 23 后添加至少一个带有 ACTION-VIEW Intent 过滤器的 Activity

android - 如何正确显示图像?

android - 如何将位图图像从 Activity 传输到 Fragment

java - 从字符串值设置 Android 图像

Android 本地化值-** 文件夹名称

java - 了解 Firebase 中的事务

java - Android MediaPlayer 中的错误代码 (1, -2147483648)

android - 让一个元素填充基于wrap_content的布局中的所有可用空间

android - 设置重力不起作用

android - 如何以编程方式在Android中将PNG图像转换为GIF