一个圆内绘制文本 [英] Draw text inside a circle

查看:133
本文介绍了一个圆内绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,我想绘制文本圈内。我想填充为白色与黑色边框和黑色文本。现在我有一个 ShapeDrawable

I'm working on an Android application and I want to draw a circle with text inside. I want the fill to be white with a black boarder and black text. Right now I have a ShapeDrawable:

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

然而,这使得整个圈白(和一个白色的背景,你不能看到它)和搜索的同时,如何你可以添加文字的形状,我似乎无法找到问题的解答之后。我还应该注意到,我将加入圈子任意数量的在每个基于用户输入不同的文本。任何帮助将是非常美联社preciated!

This however makes the whole circle white (and with a white background you can't see it) and after a while searching as to how you can add text to the shape I can't seem to find an answer that works. I should also note that I will be adding an arbitrary number of circles with different text in each based on user input. Any help would be much appreciated!

推荐答案

您可以试试这个替代方法。

You can try this alternative method.

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

Create a drawable file 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的,并设置有背景的椭圆形绘制

Then create a RelativeLayout and set the background with the oval 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>

其结果将是这样的:

The result will be something like this:

这篇关于一个圆内绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆