如何在 Android XML 可绘制文件中定义圆形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3185103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a circle shape in an Android XML drawable file?
提问by Janusz
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.
我在查找 XML for Android 中形状定义的文档时遇到了一些问题。我想在 XML 文件中定义一个填充纯色的简单圆圈,以将其包含在我的布局文件中。
Sadly the Documentation on android.com does not cover the XML attributes of the Shape classes. I think I should use an ArcShapeto 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.
遗憾的是,android.com 上的文档没有涵盖 Shape 类的 XML 属性。我想我应该使用ArcShape来画一个圆,但是没有解释如何设置从圆弧中制作圆所需的大小、颜色或角度。
回答by Arefin
This is a simple circle as a drawable in Android.
这是一个简单的圆圈作为 Android 中的可绘制对象。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
回答by M. Reza Nasirloo
Set this as your view background
将此设置为您的视图背景
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>
For solid circle use:
对于实心圆使用:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#48b3ff"/>
</shape>
Solid with stroke:
实心笔画:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#199fff"/>
<stroke
android:width="2dp"
android:color="#444444"/>
</shape>
Note: To make the oval
shape appear as a circle, in these examples, either your view that you are using this shape 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.
注意:要使oval
形状显示为圆形,在这些示例中,您认为使用此形状作为其背景的视图应该是正方形,或者您必须将形状标签的height
和width
属性设置为相等的值。
回答by Ravi Makvana
Code for Simple circle
简单圆的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2dp" android:color="#fff" />
<size android:width="80dp" android:height="80dp"/>
</shape>
回答by Riyas PK
You can use VectorDrawableas below :
您可以使用VectorDrawable如下:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="64"
android:viewportWidth="64">
<path
android:fillColor="#ff00ff"
android:pathData="M22,32
A10,10 0 1,1 42,32
A10,10 0 1,1 22,32 Z" />
</vector>
The above xml renders as :
上面的 xml 呈现为:
回答by goosemanHyman
Look in the Android SDK samples. There are several examples in the ApiDemos project:
查看 Android SDK 示例。ApiDemos 项目中有几个例子:
/ApiDemos/res/drawable/
/ApiDemos/res/drawable/
- black_box.xml
- shape_5.xml
- etc
- black_box.xml
- shape_5.xml
- 等等
It will look something like this for a circle with a gradient fill:
对于带有渐变填充的圆,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="270"/> </shape>
回答by Ashana.Hymanol
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- fill color -->
<solid android:color="@color/white" />
<!-- radius -->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- corners -->
<corners
android:radius="2dp"/>
</shape>
回答by kip2
Here's a simple circle_background.xml for pre-material:
这是一个简单的用于预材质的 circle_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/color_accent_dark" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="@color/color_accent" />
</shape>
</item>
</selector>
You can use with the attribute 'android:background="@drawable/circle_background"
in your button's layout definition
您可以与'android:background="@drawable/circle_background"
按钮布局定义中的属性一起使用
回答by Paraskevas Ntsounos
If you want a circlelike this
如果你想要一个这样的圆圈
Try using the code below:
尝试使用以下代码:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@android:color/white" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
回答by TeeTracker
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="10dp"
android:color="@color/white"/>
<gradient
android:startColor="@color/red"
android:centerColor="@color/red"
android:endColor="@color/red"
android:angle="270"/>
<size
android:width="250dp"
android:height="250dp"/>
</shape>
回答by Tienanhvn
res/drawble/circle_shape.xml
res/drawble/circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#e42828"/>
<stroke android:color="#3b91d7" android:width="5dp"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>