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

Jan*_*usz 631 android shapes android-drawable

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

可悲的是,android.com上的文档没有涵盖Shape类的XML属性.我想我应该使用ArcShape来绘制圆形,但是没有解释如何设置圆形从圆弧中所需的大小,颜色或角度.

小智 1402

这是一个简单的圆圈,作为Android中的drawable.

<?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>
Run Code Online (Sandbox Code Playgroud)

  • 以及如何动态改变颜色? (42认同)
  • 如果稍后为视图定义方形大小,则不需要`shape`中的`size`. (12认同)
  • 我试过`dp`,它被扭曲成椭圆形.对我来说,使用`pt'代替修复它. (7认同)
  • @AnkitGarg你可以从Java代码中应用一个颜色过滤器(参见Drawable类) (5认同)
  • 它的形状是椭圆而不是圆形。谁能确认? (2认同)

M. *_*loo 725

将其设置为视图背景

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#78d9ff"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于实心圆圈使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

中风坚实:

<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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

注意:要使oval形状显示为圆形,在这些示例中,您使用此形状作为背景的视图应为正方形,或者您必须将形状标记的heightwidth属性设置为相等的值.

  • 不错的解决方案。当然,如果视图的宽度和高度相同,则椭圆形将显示为圆形。我认为无论视图大小如何,都有一种使它显示为圆形的方法。 (2认同)
  • @FerranMaylinch“我认为无论视图大小如何,都可以使它显示为圆形。” 我使用RelativeLayout解决了这一问题,该方法同时包装了固定宽度/高度的ImageView(以“ src”为可绘制圆圈)和固定了文本的TextView(例如)。 (2认同)

Rav*_*ana 91

简单圆的代码

<?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>
Run Code Online (Sandbox Code Playgroud)


goo*_*ack 46

查看Android SDK示例.ApiDemos项目中有几个例子:

/ ApiDemos/RES /抽拉/

  • black_box.xml
  • shape_5.xml
  • 等等

对于带有渐变填充的圆,它看起来像这样:

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


Riy*_* PK 44

你可以使用如下的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>
Run Code Online (Sandbox Code Playgroud)

上面的xml呈现为:

在此输入图像描述

  • @Riyas 你能解释一下 pathData 部分吗?这些坐标意味着什么? (2认同)

Ash*_*kol 21

<?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>
Run Code Online (Sandbox Code Playgroud)

  • “&lt;corners /&gt;”在椭圆形中起什么作用(我认为椭圆形没有角)? (2认同)

Muj*_*han 18

尝试下面的带有破折号的代码:

<?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" />
Run Code Online (Sandbox Code Playgroud)

尝试不带破折号的代码:

<?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" />
Run Code Online (Sandbox Code Playgroud)
不带破折号 带破折号
输出无破折号 输出


kip*_*ip2 16

这是一个简单的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>
Run Code Online (Sandbox Code Playgroud)

您可以'android:background="@drawable/circle_background"在按钮的布局定义中使用该属性


小智 15

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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Par*_*nos 14

如果你想要一个像这样的圆圈

在此输入图像描述

尝试使用以下代码:

<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>
Run Code Online (Sandbox Code Playgroud)


小智 11

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>
Run Code Online (Sandbox Code Playgroud)

截屏

在此处输入图片说明


Tee*_*ker 8

<?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>
Run Code Online (Sandbox Code Playgroud)


Иво*_*дев 6

由于某种原因,我无法在 ConstraintLayout 内画一个圆圈,我只是无法使用上面的任何答案。

\n

完美工作的是一个简单的 TextView,当您按“Alt+Num7”时,会显示文本:

\n
 <TextView\n            android:layout_width="wrap_content"\n            android:layout_height="wrap_content"\n            android:textColor="#0075bc"\n            android:textSize="40dp"\n            android:text="\xe2\x80\xa2"></TextView>\n
Run Code Online (Sandbox Code Playgroud)\n


小智 6

你可以尝试使用这个

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape
        android:innerRadius="0dp"
        android:shape="ring"
        android:thicknessRatio="2"
        android:useLevel="false" >
        <solid android:color="@color/button_blue_two" />
    </shape>
</item>
Run Code Online (Sandbox Code Playgroud)

如果您将其用于文本视图,则不必担心宽度和高度的纵横比


Reh*_*han 6

首先创建Drawable资源文件

在此输入图像描述

然后将此代码添加到文件中

 <?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>
Run Code Online (Sandbox Code Playgroud)


krh*_*esh 5

你可以试试这个——

<?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>
Run Code Online (Sandbox Code Playgroud)

此外,您可以通过调整 来调整圆的半径android:thickness

在此处输入图片说明