Android Drawable Resources 之 Shape Drawable 使用详解

一、简介

Shape 是可以定义几何形状的XML文件,包括颜色和渐变等等。用作控件背景、填充色。

文件位置:res/drawable/filename.xml

语       法:


    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape=["rectangle" | "oval" | "line" | "ring"] >
        <corners
            android:radius="integer"
            android:topLeftRadius="integer"
            android:topRightRadius="integer"
            android:bottomLeftRadius="integer"
            android:bottomRightRadius="integer" />
        <gradient
            android:angle="integer"
            android:centerX="float"
            android:centerY="float"
            android:centerColor="integer"
            android:endColor="color"
            android:gradientRadius="integer"
            android:startColor="color"
            android:type=["linear" | "radial" | "sweep"]
            android:useLevel=["true" | "false"] />
        <padding
            android:left="integer"
            android:top="integer"
            android:right="integer"
            android:bottom="integer" />
        <size
            android:width="integer"
            android:height="integer" />
        <solid
            android:color="color" />
        <stroke
            android:width="integer"
            android:color="color"
            android:dashWidth="integer"
            android:dashGap="integer" />
    </shape>


二、详解

1、shape 根属性(rectangle、oval、line、ring)

     android:shape=["rectangle" | "oval" | "line" | "ring"]   
     默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)   


2、corners  设置圆角,只有当 android:shape = "rectangle" 使用

     <corners
         android:radius="dimension"      //全部的圆角半径   
         android:topLeftRadius="dimension"   //左上角的圆角半径   
         android:topRightRadius="dimension"  //右上角的圆角半径   
         android:bottomLeftRadius="dimension"    //左下角的圆角半径   
         android:bottomRightRadius="dimension" />    //右下角的圆角半径   


3、solid  设置内部填充色

     <solid android:color="color"/>


4、gradient  设置渐变色,可以定义两色渐变和三色渐变,及渐变样式

     <gradient  
         android:type=["linear" | "radial" | "sweep"]    //线性渐变(默认)、放射渐变、扫描式渐变   
         android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下   
         android:centerX="float"     //渐变中心X的相当位置,范围为0~1   
         android:centerY="float"     //渐变中心Y的相当位置,范围为0~1   
         android:startColor="color"   //渐变开始点的颜色   
         android:centerColor="color"  //渐变中间点的颜色
         android:endColor="color"    //渐变结束点的颜色   
         android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用   
         android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果  


5、stroke   设置描边属性,可以定义描边的宽度,颜色,虚实线等

     <stroke        
         android:width="dimension"   //描边的宽度   
         android:color="color"   //描边的颜色   
         android:dashWidth="dimension"   //虚线的宽度,值为0时是实线   
         android:dashGap="dimension" />      //虚线的间隔  

6、size  用来设置图形的大小

     <size   
         android:width="dimension"   
         android:height="dimension" />

7、padding  用来设置view内容距边界的距离

    <padding    
        android:left="dimension"   
        android:top="dimension"   
        android:right="dimension"   
        android:bottom="dimension" />

三、示例

1定义shape的XML文件gradient_box.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

2将这个XML文件添加到一个View

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

3、使用java代码获取shape drawable,并且添加到View

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ya-Jun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值