繁体   English   中英

如何在 Android 中向可绘制对象添加文本

[英]How to add text to drawable in Android

我想在 Drawable 文件夹中的图像上绘制一些文本。 在我的代码中,我可以通过以下方式绘制

Drawable drawable = getResources().getDrawable(R.drawable.bubble);

有没有办法通过以编程方式向这个可绘制图像添加文本?

我不确定它有多灵活,但可以使用 TextView 并为其提供如下背景:

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

或以编程方式使用setBackgroundResourcesetBackgroundDrawable

编辑:

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

我得到这样的结果:

图像中的居中文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM