如何在imageView上设置文本?

Siv*_*a K 23 android textview android-layout android-imageview

我在屏幕顶部放置了一个应用栏,它看起来如下图所示:

在此输入图像描述

现在我想在图像上设置文本,以便图像如下所示:

在此输入图像描述

如何在图像上设置文本?提前致谢

Ten*_*r04 29

如果您只想让文本居中于图像上,您只需要一个TextView,而不是图像视图.android:background="@drawable/yourImage"在TextView上设置.


Chi*_*rag 24

请尝试以下代码.

<?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/your background image">


            <LinearLayout 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent" 
                android:layout_weight="1"
                android:gravity="center" 
                android:orientation="vertical">

                <TextView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:textColor="@color/white"
                    android:textSize="18sp" 
                    android:textStyle="bold"
                    android:singleLine="true" 
                    android:text="Subject"/>

            </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这应该是一个Textable,将drawable设置为背景 (2认同)

小智 21

这就是我做到的方式,它完全按照你的要求工作:

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myImageSouce" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />
Run Code Online (Sandbox Code Playgroud)

使用相对布局


Aja*_*tel 8

  1. 使用FrameLayout (教程)
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/ImageView01"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:src="@drawable/lake"
        android:scaleType="matrix"></ImageView>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="40dp"
        android:text="@string/top_text" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/bottom_text"
        android:layout_gravity="bottom"
        android:gravity="right"
        android:textColor="#fff"
        android:textSize="50dp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

要么

2.使用图像作为背景: android:background="@drawable/yourImage"