协慌网

登录 贡献 社区

如何在 Android XML 可绘制文件中定义圆形?

我在查找 Android 版 XML 中的形状定义文档时遇到一些问题。我想在 XML 文件中定义一个用纯色填充的简单圆圈,以将其包含到布局文件中。

遗憾的是,android.com 上的文档未涵盖 Shape 类的 XML 属性。我想我应该使用ArcShape画一个圆,但是没有设置如何设置从圆弧中画出圆所需的大小,颜色或角度的说明。

答案

这是 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>

将此设置为您的视图背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#78d9ff"/>
</shape>

在此处输入图片说明

对于实心圆使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>

在此处输入图片说明

带有行程的实心:

<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>

在此处输入图片说明

注意oval形显示为圆形,或者您将这种形状用作背景的视图应该是正方形,或者必须将 shape 标签heightwidth

简单圈代码

<?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>