How to write Text on ImageView in android coding?

这一生的挚爱 提交于 2019-11-27 22:58:31

I believe the easiest workaround without overriding anything would be to have a TextView and set its background with a drawable resource.

For instance:

TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
Skaard-Solo

From the Android documentation :

BitmapDrawable(Bitmap bitmap) This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.

You can also create your own view (see Android: Creating Custom Views tutorial for an example) and put you image and the text in this view.

1). Make your own layout Say Image_TextView.xml like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imgview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:cropToPadding="true"
            android:scaleType="center"
            android:src="@drawable/box" />

        <RelativeLayout
            android:layout_width="150dp"
            android:layout_height="35dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="0dp"
            android:background="#80666666" >

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="Hello" 
                android:textColor="#FFFFFF"
                android:textStyle="bold"/>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

This will make Image with Text over it.

2). Then use layout inflator to inflate this view in your parent view.

Note : Using layout inflator you can add other view to your parent view.

its not possible to write text on imageview, but you can extend it to do so. Alternatively,

  1. you can add an element overlapping the ImageView in the layout, and make it visible only when your condition becomes true.

  2. or, you can use textView to display image, as a background or as a drawable to left/right..

Ps: however, the option 1 is easier to implement, but it will have additional rendering. so consider the other option first.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!