我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
当前回答
Android XML可绘制文件中的圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/white" />
<stroke
android:width="1.5dp"
android:color="@android:color/holo_red_light" />
<size
android:width="120dp"
android:height="120dp" />
</shape>
截图
其他回答
简单圆代码
<?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>
只需使用
ShapeDrawable circle = new ShapeDrawable( new OvalShape() );
首先创建可绘制资源文件
然后将此代码添加到文件中
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
// here define the shape
android:shape="oval">
<solid
// here define your color
android:color="#666666"/>
<size
// here define your shape size
android:width="120dp"
android:height="120dp"/>
</shape>
你可以试试这个-
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="700"
android:thickness="100dp"
android:useLevel="false">
<solid android:color="#CCC" />
</shape>
此外,您可以通过调整android:厚度来调整圆的半径。
尝试以下带破折号的代码:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="@dimen/_60sdp"
android:height="@dimen/_60sdp" />
<solid android:color="@color/black" />
<stroke
android:width="@dimen/_1sdp"
android:color="@color/white"
android:dashWidth="@dimen/_1sdp"
android:dashGap="@dimen/_1sdp" />
尝试不带破折号的代码:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="@dimen/_60sdp"
android:height="@dimen/_60sdp" />
<solid android:color="@color/black" />
<stroke
android:width="@dimen/_1sdp"
android:color="@color/white" />