在 Android 中向 ImageView 添加文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3404582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:03:13  来源:igfitidea点击:

Adding text to ImageView in Android

androidtextimageview

提问by Shashi

I want to use an ImageViewto show some message in a fancy way.

我想使用 anImageView以一种奇特的方式显示一些消息。

How do I add text to an ImageView?

如何将文本添加到ImageView?

回答by Alesqui

To add a text to your ImageViewyou can do this:

要向您添加文本,您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>

回答by Barrie Galitzky

You can also use a TextView and set the background image to what you wanted in the ImageView. Furthermore if you were using the ImageViewas a button you can set it to click-able

您还可以使用 TextView 并将背景图像设置为您想要的ImageView. 此外,如果您将ImageView用作按钮,则可以将其设置为可点击

Here is some basic code for a TextViewthat shows an image with text on top of it.

下面是一些基本代码,用于TextView显示顶部带有文本的图像。

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/your_image"
    android:text="your text here" />

回答by Sagar V.

Best Option to use text and image in a single view try this:

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

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

        android:drawableBottom="@drawable/ic_launcher"

        android:text="TextView" />

回答by Pawan Maheshwari

Use drawalbeLeft/Right/Bottom/Top in TextView to render image at respective position.

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

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

回答by Andrew S

I know this question has been and gone, but if anyone else stumbled across this I wanted to let them know. This may sound an unintuitive thing to do but you could use a button with clickable set to false or what ever. This is because a button allows one to set drawableLeft, drawableRight, drawableTop etc in addition to text.

我知道这个问题已经过去了,但如果其他人偶然发现了这个问题,我想让他们知道。这听起来可能是一件不直观的事情,但您可以使用可点击设置为 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" />

New Info:A button can have icons in drawableLeft, drawableRight, drawableTop, and drawableBottom. This makes a standard button much more flexible than an image button. The left, right, top etc is the relation to the text in the button. You can have multiple drawables on the button for example one left, one right and the text in the middle.

新信息:按钮可以在 drawableLeft、drawableRight、drawableTop 和 drawableBottom 中具有图标。这使得标准按钮比图像按钮灵活得多。左、右、上等是与按钮中文本的关系。您可以在按钮上有多个可绘制对象,例如一个左侧,一个右侧和中间的文本。

回答by Peter

To draw text directly on canvas do the following:

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

  1. Create a member Paint object in myImageViewconstructor

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

    canvas.drawText("My fancy text", xpos, ypos, mTextPaint);
    
  1. myImageView构造函数中创建成员 Paint 对象

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

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

Explore Paint and Canvas class documentation to add fancy effects.

探索 Paint 和 Canvas 类文档以添加奇特的效果。

回答by Mathias Conradt

With a FrameLayout you can place a text on top of an image view, the frame layout holding both an imageView and a textView.

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

If that's not enough and you want something more fancy like 'drawing' text, you need to draw text on a canvas - a sample is here: How to draw RTL text (Arabic) onto a Bitmap and have it ordered properly?

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

回答by Madhu Sudhana Reddy Kasireddy

    <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>

回答by jitain sharma

You can use the TextView for the same purpose, But if you want to use the same with the ImageView then you have to create a class and extends the ImageView then use onDraw() method to paint the text on to the canvas. for more details visit to http://developer.android.com/reference/android/graphics/Canvas.html

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