简体   繁体   English

如何在Android中使用xml绘制形状

[英]How to draw shape using xml in android

Need to draw the following circle , with the red colour shape using xml. 需要使用xml使用红色绘制以下圆圈。

在此处输入图片说明

Is it possible to draw this using xml only? 是否可以仅使用xml进行绘制?

You can make circle using the following XML code: 您可以使用以下XML代码进行圈出:

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

You can add the above circle as the background to a view, and on top of that view you could keep another view, which could be center vertical, and it's XML would be: 您可以将上面的圆圈作为背景添加到视图中,并在该视图之上可以保留另一个视图,该视图可以居中垂直,其XML为:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <solid android:color="#FF0000"  />
    <padding android:bottom="1dp" android:left="10dp" android:right="10dp" android:top="1dp"/>
    <corners
        android:bottomRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>
</shape>

to draw circle programmatically you can use this way,this worked for me 以编程方式画圆,您可以使用这种方式,这对我有用

ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
                biggerCircle.setIntrinsicHeight( 60 );
                biggerCircle.setIntrinsicWidth( 60);
                biggerCircle.setBounds(new Rect(30, 30, 30, 30));
                biggerCircle.getPaint().setColor(Color.parseColor(first));//give any color here
                holder.firstcolor.setBackgroundDrawable(biggerCircle); 

I have create the similar shape where an outer gray circle contains red circle inside. 我创建了类似的形状,其中外部灰色圆圈包含内部红色圆圈。 Here is the code: 这是代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#E84F3D" />
<stroke
    android:width="15dp"
    android:color="#BEBEBE" />

</shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM