Android 在圆内绘制文本

Android 在圆内绘制文本,android,drawable,Android,Drawable,我正在开发一个Android应用程序,我想画一个圆圈,里面有文字。我希望填充为白色,带有黑色边框和黑色文本。现在我有一个可变形的: mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(0xFFFFFF); 然而,这会使整个圆圈变白(白色背景下你看不到它),并且在搜索了一段时间后,关于如何将文本添加到形状中,我似乎找不到有效的答案。我还应该注意,我将根据用户输入添加任意数量的圆圈,每个圆圈中有

我正在开发一个Android应用程序,我想画一个圆圈,里面有文字。我希望填充为白色,带有黑色边框和黑色文本。现在我有一个
可变形的

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xFFFFFF);

然而,这会使整个圆圈变白(白色背景下你看不到它),并且在搜索了一段时间后,关于如何将文本添加到形状中,我似乎找不到有效的答案。我还应该注意,我将根据用户输入添加任意数量的圆圈,每个圆圈中有不同的文本。任何帮助都将不胜感激

您可以尝试这种替代方法

创建一个可绘制文件oval.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
   <solid android:color="#fff"/>
   <stroke android:width="2px" android:color="#000"/>
</shape>

然后创建一个RelativeLayout,并使用椭圆形可绘制图形设置背景

<RelativeLayout
    android:id="@+id/circle"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:background="@drawable/oval" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/hello_world" />
</RelativeLayout>

结果会是这样的:


我肯定很晚才在这里回复,但这可能对其他人有所帮助

    ShapeDrawable colorCode = new ShapeDrawable(new OvalShape());
    colorCode.getPaint().setStyle(Paint.Style.FILL); //See more paint style for border circle etc. like STROKE
    colorCode.getPaint().setAntiAlias(true);
    colorCode.getPaint().setColor(getResources().getColor(YOUR_COLOUR_HERE_FROM_XML));
    colorCode.setIntrinsicHeight(Globals.dp2px(5, getActivity())); //converting dp to px, you can just put any integer instead of dp2px method
    colorCode.setIntrinsicWidth(Globals.dp2px(5, getActivity()));
    greenText.setBackgroundDrawable(colorCode);

Drawable不是用来画文本的,你应该试试位图,就像Geralt Illustrated什么???Drawable用于绘制可以使用画布绘制的所有内容。我编辑了我的问题,以添加关键信息,因为我需要多个圆圈,每个圆圈中有不同的文本。这将必须通过编程来完成,因此我不能简单地将它们硬编码到xml文件中。这个答案非常好,因为在您的电脑中,有人告诉您将椭圆形可绘制文件设置为TextView的背景。但是,如果不绘制填充圆,则TextView会接触圆的边。因此,在TextView周围使用RelativeLayout的方法要好得多!如果您不需要在背景周围使用大纲,请不要将RelativeLayout用作TextView的容器。在大多数情况下,这是不需要的。
setBackgroundDrawable
现在已被弃用,但只需替换为
setBackground