Android中Shape Drawable在xml中的使用

关于Shape使用的官方文档:

http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Android中常常使用shape来定义控件的一些显示属性,下面是Shape中的所有属性及一个简单的示例:

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape      
  3.     xmlns:android="http://schemas.android.com/apk/res/android"      
  4.     android:shape=["rectangle" | "oval" | "line" | "ring"] >      
  5.     <corners          
  6.         android:radius="integer"          
  7.         android:topLeftRadius="integer"          
  8.         android:topRightRadius="integer"          
  9.         android:bottomLeftRadius="integer"          
  10.         android:bottomRightRadius="integer" />      
  11.     <gradient          
  12.         android:angle="integer"          
  13.         android:centerX="integer"          
  14.         android:centerY="integer"          
  15.         android:centerColor="integer"          
  16.         android:endColor="color"          
  17.         android:gradientRadius="integer"          
  18.         android:startColor="color"          
  19.         android:type=["linear" | "radial" | "sweep"]          
  20.         android:useLevel=["true" | "false"] />      
  21.     <padding          
  22.         android:left="integer"          
  23.         android:top="integer"          
  24.         android:right="integer"          
  25.         android:bottom="integer" />      
  26.     <size          
  27.         android:width="integer"          
  28.         android:height="integer" />      
  29.     <solid          
  30.         android:color="color" />      
  31.     <stroke          
  32.         android:width="integer"          
  33.         android:color="color"          
  34.         android:dashWidth="integer"          
  35.         android:dashGap="integer" />  
  36. </shape>  


下面是一个小示例:

main.xml文件内容如下:

[html]  view plain  copy
 print ?
  1. <p><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingLeft="40dip" ></p><p>    <Button  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:background="@drawable/button_selector"  
  10.         android:text="TestShapeButton" /></p><p></LinearLayout></p>  


 

button_selector.xml内容如下:

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:state_pressed="true"><shape>  
  5.   
  6.             <!-- 渐变 -->  
  7.             <gradient android:endColor="#FFFFFF" android:gradientRadius="50" android:startColor="#ff8c00" android:type="radial"/>  
  8.             <!-- 描边 -->  
  9.             <stroke android:dashGap="3dp" android:dashWidth="5dp" android:width="2dp" android:color="#dcdcdc" />  
  10.             <!-- 圆角 -->  
  11.             <corners android:radius="2dp" />  
  12.   
  13.             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
  14.         </shape></item>  
  15.     <item android:state_focused="true"><shape>  
  16.             <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />  
  17.   
  18.             <stroke android:width="2dp" android:color="#dcdcdc" />  
  19.   
  20.             <corners android:radius="2dp" />  
  21.   
  22.             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
  23.         </shape></item>  
  24.     <item><shape>  
  25.             <solid android:color="#ff9d77" />  
  26.   
  27.             <stroke android:width="2dp" android:color="#fad3cf" />  
  28.   
  29.             <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />  
  30.   
  31.             <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />  
  32.         </shape></item>  
  33.   
  34. </selector>  


 

效果图如下:

 一般状态:

 

按下状态:


原文地址:http://blog.csdn.net/ygc87/article/details/7673587

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值