我正在开发一个Android应用程序,我想用里面的文字画一个圆圈。 我希望填充是白色的黑色边框和黑色的文字。 现在我有一个ShapeDrawable
:
mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(0xFFFFFF);
这然而使整个圆圈白色(和白色的背景,你不能看到它),并经过一段时间search如何可以添加文字的形状,我似乎无法find一个有效的答案。 我还应该注意到,我将根据用户input添加任意数量的不同文字的圆圈。 任何帮助将非常感激!
你可以尝试这种替代方法。
创build一个可绘制的文件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>
然后创build一个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);