在圆圈内绘制文字

Rya*_*les 9 android drawable

我正在开发一个Android应用程序,我想绘制一个带有文本的圆圈.我希望填充为白色,黑色边框和黑色文字.现在我有一个ShapeDrawable:

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xFFFFFF);
Run Code Online (Sandbox Code Playgroud)

然而,这会使整个圆圈变白(并且白色背景你看不到它),经过一段时间的搜索,你怎么能在形状上添加文字,我似乎无法找到有效的答案.我还要注意,我将根据用户输入添加任意数量的不同文本的圆圈.任何帮助将非常感激!

Joe*_*des 18

您可以尝试这种替代方法.

创建一个可绘制的文件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>
Run Code Online (Sandbox Code Playgroud)

然后创建一个RelativeLayout并使用椭圆drawable设置背景

<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>
Run Code Online (Sandbox Code Playgroud)

结果将是这样的:

在此输入图像描述

  • 这个答案很棒,因为在http://stackoverflow.com/questions/10060470/android-draw-circle-around-text中,您被告知将椭圆可绘制设置为TextView的背景.但是,如果您不绘制实心圆,则TextView会触摸圆的边缘.因此,围绕TextView的RelativeLayout方法要好得多! (2认同)