How to define a circle shape in an android xml drawable file?

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I have some problems finding the documentation of the definitions of shapes in XML for Android. I would like to define a simple circle filled with a solid color in an XML File to include it into my layout files.

Sadly the Documentation on android.com does not cover the XML attributes of the Shape classes. I think I should use an ArcShape to draw a circle but there is no explanation on how to set the size, the color, or the angle needed to make a circle out of an Arc.

回答1:

This is a simple circle as a drawable in Android.



回答2:

Set this as your view background

For solid circle use:

Solid with stroke:

Note: To make the oval shapes appear as circle, in these examples, either your view that your are using these shapes as its background should be a square or you have to set the height and width properties of the shape tag to an equal value.



回答3:

Code for Simple circle



回答4:

Look in the Android SDK samples. There are several examples in the ApiDemos project:

/ApiDemos/res/drawable/

  • black_box.xml
  • shape_5.xml
  • etc

It will look something like this for a circle with a gradient fill:

 


回答5:

Use this



回答6:

Here's a simple circle_background.xml for pre-material:

You can use with the attribute 'android:background="@drawable/circle_background" in your button's layout definition



回答7:



回答8:



回答9:

If you want a circle try using the code below:



回答10:

Just use

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() ); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!