shape和selector的 使用

来源:互联网 发布:淘宝怎么找人刷信誉 编辑:程序博客网 时间:2024/05/16 00:42

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector。可以这样说,shape和selector在美化控件中的作用是至关重要的。

1.Shape

简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

属性:

<shape>  android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient>  渐变

android:startColor  起始颜色

android:endColor  结束颜色             

android:angle  渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;

android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  填充

android:color  填充的颜色

<stroke > 描边

android:width 描边的宽度

android:color 描边的颜色

android:dashWidth 表示'-'横线的宽度

android:dashGap 表示'-'横线之间的距离

<corners > 圆角

android:radius  圆角的半径 值越大角越圆

android:topRightRadius  右上圆角半径
  
android:bottomLeftRadius 右下圆角角半径
 
android:topLeftRadius 左上圆角半径

android:bottomRightRadius 左下圆角半径
 

 2.Selector

 简介

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

  1. <?xml version="1.0" encoding="utf-8" ?>     
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <!-- 默认时的背景图片-->    
  4. <item android:drawable="@drawable/pic1" />      
  5.  
  6. <!-- 没有焦点时的背景图片 -->    
  7. <item 
  8. android:state_window_focused="false"
  9. android:drawable="@drawable/pic_blue" 
  10. />     
  11.  
  12. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->    
  13. <item 
  14. android:state_focused="true" 
  15. android:state_pressed="true"   
  16. android:drawable"@drawable/pic_red" 
  17. />   
  18.  
  19. <!-- 触摸模式下单击时的背景图片-->    
  20. <item 
  21. android:state_focused="false" 
  22. android:state_pressed="true"   
  23. android:drawable="@drawable/pic_pink" 
  24. />    
  25.  
  26. <!--选中时的图片背景-->    
  27. <item 
  28. android:state_selected="true" 
  29. android:drawable="@drawable/pic_orange" 
  30. />     
  31.  
  32. <!--获得焦点时的图片背景-->    
  33. <item 
  34. android:state_focused="true" 
  35. android:drawable="@drawable/pic_green" 
  36. />     
  37. </selector> 
http://blog.csdn.net/YUZHIBOYI/article/details/32709903
0 0