android shape的使用


http://www.cnblogs.com/cyanfei/archive/2012/07/27/2612023.html

http://blog.csdn.net/lan410812571/article/details/9946991

shape用于设定形状,可以在selector,layout等里面使用,有6个子标签,各属性如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- 圆角 -->
    <corners
        android:radius="9dp"
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp"/><!-- 设置圆角半径 -->
    
    <!-- 渐变 -->
    <gradient
        android:startColor="@android:color/white"
        android:centerColor="@android:color/black"
        android:endColor="@android:color/black"
        android:useLevel="true"
        android:angle="45"
        android:type="radial"
        android:centerX="0"
        android:centerY="0"
        android:gradientRadius="90"/>
    
    <!-- 间隔 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp"/><!-- 各方向的间隔 -->
    
    <!-- 大小 -->
    <size
        android:width="50dp"
        android:height="50dp"/><!-- 宽度和高度 -->
    
    <!-- 填充 -->
    <solid
        android:color="@android:color/white"/><!-- 填充的颜色 -->
    
    <!-- 描边 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
        android:dashWidth="1dp"
        android:dashGap="2dp"/>
    
</shape>
复制代码

填充:设置填充的颜色

间隔:设置四个方向上的间隔

大小:设置大小

圆角:同时设置五个属性,则Radius属性无效

android:Radius="20dp"                           设置四个角的半径

android:topLeftRadius="20dp"              设置左上角的半径 
android:topRightRadius="20dp"           设置右上角的半径 
android:bottomLeftRadius="20dp"      设置右下角的半径 
android:bottomRightRadius="20dp"    设置左下角的半径

描边:dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框

android:width="20dp"                               设置边边的宽度 
android:color="@android:color/black"  设置边边的颜色 
android:dashWidth="2dp"                         设置虚线的宽度 
android:dashGap="20dp"                          设置虚线的间隔宽度

渐变:当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错。android:useLevel 这个属性不知道有什么用。

angle对应值的起点如图:

                                                                   Z@QTDNQ0_9)0VFC5ZT8DT8B




有图又真相,先上图再说。


点击效果:




设置虚线:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="line" >  
  4.     <stroke  
  5.         android:dashGap="3dp"  
  6.         android:dashWidth="6dp"  
  7.         android:width="1dp"  
  8.         android:color="#63a219" />  
  9.     <!-- 虚线的高度 -->  
  10.     <size android:height="1dp" />  
  11. </shape>  

其中,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线


设置圆角:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle">  
  4.     <!-- 填充颜色 -->  
  5.     <solid android:color="#FFFFFF"></solid>  
  6.     <!-- 线的宽度,颜色灰色 -->  
  7.     <stroke android:width="1dp" android:color="#63a219"></stroke>          
  8.     <!-- 矩形的圆角半径 -->  
  9.     <corners android:radius="10dp" />         
  10. </shape>  

设置渐变填充和四个圆角半径:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  2.     android:shape="rectangle">  
  3.     <!--分別對應上面左圆角的半径,上面右圆角的半径,下面左圆角的半径,下面右圆角的半径-->  
  4.     <corners    
  5.           android:topLeftRadius="0dp"  
  6.           android:topRightRadius="7dp"  
  7.           android:bottomLeftRadius="0dp"  
  8.           android:bottomRightRadius="7dp"/>  
  9.     <!--設置漸變-->  
  10.     <gradient android:startColor="#9cff00"   
  11.           android:endColor="#197600"  
  12.           android:angle="270"/>  
  13.     <stroke     
  14.         android:width="1dp"   
  15.         android:color="#63a219" />   
  16. </shape>  


设置渐变点击效果:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <style name="list_item_top">  
  2.     <item name="android:clickable">true</item>  
  3.     <item name="android:focusable">true</item>  
  4.     <item name="android:paddingTop">10dip</item>  
  5.     <item name="android:paddingBottom">10dip</item>  
  6.     <item name="android:paddingLeft">10dip</item>  
  7.         <item name="android:paddingRight">10dip</item>  
  8.         <item name="android:gravity">center_vertical</item>  
  9.         <item name="android:background">@drawable/background_view_rounded_top</item>  
  10. </style>  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <inset xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:insetLeft="1.0px"  
  4.     android:insetRight="1.0px" >  
  5.   
  6.     <selector>  
  7.         <item android:state_pressed="true">  
  8.             <shape>  
  9.                 <gradient  
  10.                     android:angle="270.0"  
  11.                     android:endColor="@color/base_end_color_pressed"  
  12.                     android:startColor="@color/base_start_color_pressed" />  
  13.   
  14.                 <corners  
  15.                     android:bottomLeftRadius="0.0dip"  
  16.                     android:bottomRightRadius="0.0dip"  
  17.                     android:radius="2.0dip"  
  18.                     android:topLeftRadius="10.0dip"  
  19.                     android:topRightRadius="10.0dip" />  
  20.                   
  21.                 <stroke   
  22.                     android:width="1dp"   
  23.                     android:color="#eededede" />  
  24.             </shape>  
  25.         </item>  
  26.         <item>  
  27.             <shape>  
  28.                 <gradient  
  29.                     android:angle="270.0"  
  30.                     android:endColor="@color/base_end_color_default"  
  31.                     android:startColor="@color/base_start_color_default" />  
  32.   
  33.                 <corners  
  34.                     android:bottomLeftRadius="0.0dip"  
  35.                     android:bottomRightRadius="0.0dip"  
  36.                     android:radius="2.0dip"  
  37.                     android:topLeftRadius="11.0dip"  
  38.                     android:topRightRadius="11.0dip" />  
  39.                   
  40.                 <stroke   
  41.                     android:width="1dp"   
  42.                     android:color="#eededede" />  
  43.             </shape>  
  44.         </item>  
  45.     </selector>  
  46.   
  47. </inset>  



重新补充:好久没有关注自己的博客,没有注意到各位的评论,关于4.0以上设备虚线会变实线的问题,下面几位仁兄已经给出了答案,

代码中可以添加:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);  

xml中可以添加:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. android:layerType="software"  

谢谢大家的参与!


源码免费下载地址:免费下载

http://download.csdn.net/detail/lan410812571/5925371


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值