[mobilesafe] 10_资源Shape Drawable

Android 4.0

资源Shape Drawable

http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
1、在res/drawable/filename.xml

2、定义语法
<?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="integer"
       
android:centerY="integer"
       
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  //固定颜色,和gradient相对立,只能配置一个
       
android:color="color" />
   
<stroke   //边框
       
android:width="integer"
       
android:color="color"
       
android:dashWidth="integer" // 实线宽度,间隔宽度,和android:dashGap才有效果
       
android:dashGap="integer" /> //中间间隙宽度
</shape>
样例:
a) \res\drawable\my_shape_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >     //android:shape="oval" 形状

    <corners    //边角弧度
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="5dip"
        android:topLeftRadius="10dip"
        android:topRightRadius="5dip" />

    <gradient  //渐变色
        android:endColor="#0066ff00"  //开始颜色
        android:startColor="#66ff00" />  //结束颜色

    <padding  //内容距边框的距离
        android:bottom="50dip"
        android:left="15dip"
        android:right="15dip"
        android:top="50dip" />

    <size   //边框的大小
        android:height="100dip"
        android:width="200dip" />

    <stroke  //边框的属性
           android:color="#ff0000" //边框颜色
        android:width="1dip"  //边框粗细
        android:dashWidth="50dip" //实线宽度
        android:dashGap="10dip"  //虚线宽度
         />
</shape>
b)  activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/my_shape_selector"
        android:gravity="center"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/my_shape_selector"
        android:gravity="center"
        android:text="Button" />
</LinearLayout>
c) 结果