在Android中向ImageView添加文本

57

我想使用ImageView以华丽的方式显示一些消息。

如何向ImageView添加文本?

9个回答

98

要向您的 ImageView 添加文本,您可以执行以下操作:

<RelativeLayout> // Only works inside a RelativeLayout
    <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" />
 </RelativeLayout>

9
请将文本放置在RelativeLayout中。 - Alesqui
2
最好在帖子中包含这个PS。没有您的PS就无法正常工作 :) - mente
7
建议使用其他答案中建议的TextView,因为这种方法会创建额外的布局,不推荐使用。 - Tautvydas
2
我之前不得不使用这个,但是改用TextView解决了问题,而且更简单,我想。 - George

34
你还可以使用 TextView 并将背景图像设置为你想要的 ImageView。此外,如果你将 ImageView 用作按钮,则可以将其设置为可点击。
以下是一个显示文本在图像上方的 TextView 的基本代码。
<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/your_image"
    android:text="your text here" />

3
这个问题在于背景无法按比例缩放。 - Jesus Dimrix
1
@IgorGanapolsky:什么?!它在所有API级别上都可用,从1开始。 - Phantômaxx

25

使用文本和图像的最佳选择是将它们放在单个视图中,您可以尝试以下方法:

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:drawableBottom="@drawable/ic_launcher"

        android:text="TextView" />

17

在TextView中使用drawableLeft/Right/Bottom/Top来渲染相应位置的图像。

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/image"    
        android:text="@strings/text" 
        />

5
图片将与TextView相邻,但不会覆盖在其上方。 - IgorGanapolsky
最佳答案是,在TextView中使用drawableTop、drawableBottom等属性。 - Obtice

11
我知道这个问题已经过去了,但如果有人偶然发现它,我想让他们知道。这可能听起来是一件不直观的事情,但是您可以使用一个按钮,将其点击设置为false或任何其他值。这是因为按钮允许设置drawableLeft、drawableRight、drawableTop等,除了文本之外。
  <Button
  android:id="@+id/button1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/border_box1"
  android:drawableLeft="@drawable/ar9_but_desc"
  android:padding="20dp"
  android:text="@string/ar4_button1"
  android:textColor="@color/white"
  android:textSize="24sp" />

新信息: 一个按钮可以在drawableLeft、drawableRight、drawableTop和drawableBottom中拥有图标,这使得普通按钮比图像按钮更加灵活。左侧、右侧、顶部等是相对于按钮中的文本的位置关系。你可以在按钮上有多个可绘制物,例如一个在左侧,一个在右侧,中间有文本。


这里的android:drawableLeft是什么意思? - IgorGanapolsky
1
我已在上方添加了一些注释。 - Andrew S

6

要直接在画布上绘制文本,请按照以下步骤进行:

  1. Create a member Paint object in myImageView constructor

    Paint mTextPaint = new Paint();
    
  2. Use canvas.drawText in your myImageView.onDraw() method:

    canvas.drawText("My fancy text", xpos, ypos, mTextPaint);
    

浏览Paint和Canvas类文档,以添加花哨的效果。


您能否帮我解决相反的问题,也就是如何在Android中获取现有ImageView的文本(以上述方式绘制)? - aida

3

2
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_weight="30" >

        <ImageButton
            android:id="@+id/imgbtnUploadPendingPods"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/hello_world"
            android:src="@drawable/upload_icon" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:paddingTop="30dp"
            android:text="@string/pendingpods"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </FrameLayout>

不用担心,只需在FRAME LAYOUT下使用图像按钮,并将TEXT VIEW拖到图像按钮上即可。完成后,如果您觉得这篇文章有用,请访问我的网站www.krsolutions.in并点击底部的“赞”按钮。 - Madhu Sudhana Reddy Kasireddy

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接