56

我想用ImageView一个花哨的方式显示一些信息。

如何将文本添加到ImageView?

4

9 回答 9

93

要向您添加文本,您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>
于 2011-11-25T15:24:18.880 回答
32

您还可以使用 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" />
于 2012-09-17T01:49:12.767 回答
25

在单个视图中使用文本和图像的最佳选项试试这个:

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

        android:drawableBottom="@drawable/ic_launcher"

        android:text="TextView" />
于 2013-07-01T06:28:02.090 回答
16

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

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/image"    
        android:text="@strings/text" 
        />
于 2013-07-30T06:29:20.747 回答
9

我知道这个问题已经过去了,但是如果其他人偶然发现了这个问题,我想让他们知道。这可能听起来不直观,但您可以使用可点击设置为 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 中有图标。这使得标准按钮比图像按钮更灵活。左、右、上等是按钮中文本的关系。您可以在按钮上有多个可绘制对象,例如一个左侧、一个右侧和中间的文本。

于 2013-10-04T07:23:27.930 回答
6

要直接在画布上绘制文本,请执行以下操作:

  1. myImageView在构造函数中创建成员 Paint 对象

    Paint mTextPaint = new Paint();
    
  2. canvas.drawText在您的方法中使用myImageView.onDraw()

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

探索 Paint 和 Canvas 类文档以添加精美的效果。

于 2012-03-14T14:07:03.357 回答
3

使用 FrameLayout,您可以在图像视图顶部放置文本,框架布局同时包含 imageView 和 textView。

如果这还不够,并且您想要更花哨的东西,例如“绘制”文本,则需要在画布上绘制文本 - 示例如下:如何在位图上绘制 RTL 文本(阿拉伯语)并正确排序?

于 2010-08-04T11:11:16.193 回答
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>
于 2013-05-24T12:16:59.173 回答
1

您可以将 TextView 用于相同的目的,但如果您想对 ImageView 使用相同的功能,则必须创建一个类并扩展 ImageView,然后使用 onDraw() 方法将文本绘制到画布上。有关更多详细信息,请访问http://developer.android.com/reference/android/graphics/Canvas.html

于 2012-10-03T18:35:51.743 回答