安卓图片上文字

100

我有一个带有图片的imageView,在这张图片上我想要放置一段文字。我该如何实现?


1
简单的方法是使用textView并像在ImageView中设置背景一样设置它的背景。这将使您的工作变得更加容易。 - Zar E Ahmer
请查看以下链接。希望对您有所帮助...https://dev59.com/Xmgu5IYBdhLWcg3wv5mm#26064945 - Ganesh Katikar
9个回答

168

这是我如何做到的,它在RelativeLayout内完全按照您的要求工作:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

非常感谢这个例子。我试图使用compoundedDrawable textview,但是我无法设置图片的URL。但是,在相对布局中使用这种解决方法非常重要,以便识别layout_align设置。 - AntonSack
将控件的ID放到对齐方式中解决了很多问题,谢谢。 - jack

19

也许你可以换个角度考虑:在背景上添加一个可绘制的TextView似乎更容易:

 <TextView
            android:id="@+id/text"
            android:background="@drawable/rounded_rectangle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        </TextView>

10
这个答案的问题在于你无法控制背景属性,比如说imageView.scale。 - Jesus Dimrix
1
创建一个可绘制对象可以让你做到这一点,希望能对你有所帮助,@JesusDimrix。 - Moses Wangira

7

7
您可以创建一个继承自ImageView类的新类,并覆盖onDraw方法。在该方法中首先调用super.onDraw(),然后绘制您想要显示的一些文本。如果您这样做,您可以将其用作单个布局组件,这使得与其他组件一起布局更容易。

6

有多种方法可供选择。您可以使用RelativeLayout或AbsoluteLayout。

使用RelativeLayout,例如,您可以使图像与父级在左侧对齐,还可以使文本视图左对齐... 然后,您可以在文本视图上使用边距、填充和引力来使其在所需位置上覆盖图像。


1
我偶然发现了这个方法。但是当你想在一个imagebutton或者imageview上放置一些文本时,它非常方便。例如,在当前天气的图像上放置当前温度。 - Phobos

4

对于此问题,您可以使用一个TextView和android:drawableLeft/Right/Top/Bottom将图像定位到TextView中。此外,您还可以使用android:drawablePadding=""在TextView和Drawable之间添加一些填充。

使用方式如下:

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

    android:drawableBottom="@drawable/yourDrawable"
    android:drawablePadding="10dp" 
    android:text="Look at the drawable below"/>

通过这种方式,您不需要额外的ImageView。还可以在TextView的多个边缘上使用两个Drawable。

使用此方法唯一的问题是,Drawable无法像ImageView那样进行缩放。


3

尝试下面的代码,这将会对你有所帮助。

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/gallery1"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#7ad7d7d7"
        android:gravity="center"
        android:text="Juneja Art Gallery"
        android:textColor="#000000"
        android:textSize="15sp"/>
</RelativeLayout>

3
您可以使用TextView并将其背景更改为您想使用的图片。

这个答案存在的问题是,你无法控制背景属性,比如imageView.scale。 - Jesus Dimrix

1
以下代码将帮助您。
public class TextProperty {
    private int heigt;                              //读入文本的行数
    private String []context = new String[1024];    //存储读入的文本

    /*
     *@parameter wordNum
     *
     */
    public TextProperty(int wordNum ,InputStreamReader in) throws Exception {
        int i=0;
        BufferedReader br = new BufferedReader(in);
        String s;
        while((s=br.readLine())!=null){
            if(s.length()>wordNum){
                int k=0;
                while(k+wordNum<=s.length()){
                    context[i++] = s.substring(k, k+wordNum);
                    k=k+wordNum;
                }
                context[i++] = s.substring(k,s.length());
            }
            else{
                context[i++]=s;
            }
        }
        this.heigt = i;
        in.close();
        br.close();
    }


    public int getHeigt() {
        return heigt;
    }

    public String[] getContext() {

        return context;
    }
}

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ImageView iv;
    private final int WORDNUM = 35;  //转化成图片时  每行显示的字数
    private final int WIDTH = 450;   //设置图片的宽度

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.imageView);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int x=5,y=10;
                try {
                    TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
                    Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    Paint paint = new Paint();
                    paint.setColor(Color.WHITE);
                    paint.setTextAlign(Paint.Align.LEFT);
                    paint.setTextSize(20f);

                    String [] ss = tp.getContext();
                    for(int i=0;i<tp.getHeigt();i++){
                        canvas.drawText(ss[i], x, y, paint);
                        y=y+20;
                    }

                    canvas.save(Canvas.ALL_SAVE_FLAG);
                    canvas.restore();
                    String path = Environment.getExternalStorageDirectory() + "/image.png";
                    System.out.println(path);
                    FileOutputStream os = new FileOutputStream(new File(path));
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                    //Display the image on ImageView.
                    iv.setImageBitmap(bitmap);
                    iv.setBackgroundColor(Color.BLUE);
                    os.flush();
                    os.close();
                }
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}```

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