【问题标题】:How to draw shape using xml in android如何在android中使用xml绘制形状
【发布时间】:2016-01-15 05:36:48
【问题描述】:

需要用xml画出下面的圆形,红色的形状。

是否可以仅使用 xml 进行绘制?

【问题讨论】:

    标签: android android-xml android-drawable xml-drawable


    【解决方案1】:

    您可以使用以下XML 代码制作圆圈:

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

    您可以将上述圆圈作为背景添加到视图中,并在该视图的顶部保留另一个视图,该视图可以是中心垂直的,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>
    
    【解决方案2】:

    以编程方式绘制圆圈,您可以使用这种方式,这对我有用

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

    【讨论】:

      【解决方案3】:

      我已经创建了类似的形状,其中外部灰色圆圈包含内部红色圆圈。代码如下:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多