[英]How to add text to drawable in Android
I want to draw some text on image which is in Drawable folder.我想在 Drawable 文件夹中的图像上绘制一些文本。 In my code I am getting drawable in the following way
在我的代码中,我可以通过以下方式绘制
Drawable drawable = getResources().getDrawable(R.drawable.bubble);
Is there any way to add text through code that is programmatically to this drawable image?有没有办法通过以编程方式向这个可绘制图像添加文本?
I'm not sure how flexible it is but it is possible to use a TextView and give it a background like so:我不确定它有多灵活,但可以使用 TextView 并为其提供如下背景:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text"
android:background="@+drawable/image"
/>
or programmatically with setBackgroundResource or setBackgroundDrawable .或以编程方式使用setBackgroundResource或setBackgroundDrawable 。
Edit:编辑:
With an xml layout file like this:使用 xml 布局文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Hello!"
android:background="@+drawable/image"
android:gravity="center"
/>
</LinearLayout>
I get a result like this:我得到这样的结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.