Android之Selector、Shape介绍

------------整理自网络----------------------

 
  1. <?xml version=”1.0″ encoding=”utf-8″?>   
  2. <shape xmlns:android=”http://schemas.android.com/apk/res/android”>   
  3. <solid android:color=”#00000000″/>   
  4. <stroke android:width=”1dp” color=”#ff000000″/>   
  5. <padding  android:left=”1dp”   
  6. android:top=”1dp”   
  7. android:right=”1dp”   
  8. android:bottom=”1dp” />   
  9. </shape>   
  10.   
  11. solid android:color=“”  //使用这种颜色全部实心填充   
  12.   
  13. stroke  描边   
  14.   
  15. android:width=“1dp” color=“#ff000000” 边的颜色是#ff000000,宽度为1dp   
  16.   
  17. padding  间隔 距离上下左右边框的距离为1dp   
  18.   
  19. 在开发的过程中你还会用到   
  20.   
  21. gradient   此属性控制布局的渐变颜色   
  22.   
  23. 如<gradient android:startColor=”#ff0000″   
  24.   
  25. android:endColor=”#ffffff”            设置渐变颜色,从#ff0000渐变到#ffffff   
  26.   
  27. android:angle=”90″                      设置渐变角度必须为45度得整数倍   
  28.   
  29. android:type=”linear”                   将渐变模式设置成线性模式   
  30.   
  31. >   
  32.   
  33. corners 属性设置边角角度   
  34.   
  35. <corners   
  36.   
  37. android:topRightRadius=”20dp”    右上角   
  38. android:bottomLeftRadius=”20dp”    右下角   
  39. android:topLeftRadius=”1dp”    左上角   
  40. android:bottomRightRadius=”0dp”    左下角   
  41.   
  42. >  

  1. <?xml version="1.0" encoding="utf-8"?>   
  2.   
  3.     <selector xmlns:android="http://schemas.android.com/apk/res/android" >   
  4.   
  5.         <item   
  6.   
  7.             android:color="hex_color"  
  8.   
  9.             android:state_pressed="true/false"  
  10.   
  11.  “true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。   
  12.   
  13.             android:state_focused="true/false"  
  14.   
  15. true”表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button);“false”表示非聚焦状态使用。   
  16.   
  17.             android:state_selected="true/false"  
  18.   
  19. true”表示选中状态使用(例如Tab打开);“false”表示非选中状态使用。   
  20.   
  21.             android:state_active="true/false"  
  22.   
  23. true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)   
  24.   
  25.             android:state_checkable="true/false"  
  26.   
  27.  “true”表示勾选状态使用;“false”表示非勾选状态使用。   
  28.   
  29.             android:state_checked="true/false"  
  30.   
  31. true”表示勾选状态使用;“false”表示非勾选状态使用。   
  32.   
  33.             android:state_enabled="true/false"  
  34.   
  35. true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。   
  36.   
  37.             android:state_window_focused="true/false"  
  38.   
  39. true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如Notification栏拉下或对话框显示)。   
  40.   
  41. />   
  42.   
  43.     </selector>   
  44.   
  45.     
  46.   
  47.     
  48.   
  49. shape的结构描述:   
  50.   
  51.  <shape>     
  52.   
  53.     <!-- 实心 -->     
  54.   
  55.     <solid android:color="#ff9d77"/>     
  56.   
  57.     <!-- 渐变 -->     
  58.   
  59.     <gradient     
  60.   
  61.         android:startColor="#ff8c00"  <!—开始颜色 -->     
  62.   
  63.         android:endColor="#FFFFFF"  <!—结束颜色 -->     
  64.   
  65.         android:angle="270" />     
  66.   
  67.     <!-- 描边 -->     
  68.   
  69.     <stroke     
  70.   
  71.         android:width="2dp"     
  72.   
  73.         android:color="#dcdcdc" />     
  74.   
  75.     <!-- 圆角 -->     
  76.   
  77.     <corners     
  78.   
  79.         android:radius="2dp" />     
  80.   
  81.     <padding     
  82.   
  83.         android:left="10dp"     
  84.   
  85.         android:top="10dp"     
  86.   
  87.         android:right="10dp"     
  88.   
  89.         android:bottom="10dp" />     
  90.   
  91. </shape>     
  92.   
  93.     
  94.   
  95. 下面是上面属性的说明   
  96.   
  97. solid:实心,就是填充的意思   
  98.   
  99. android:color指定填充的颜色   
  100.   
  101.     
  102.   
  103. gradient:渐变   
  104.   
  105. android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。   
  106.   
  107. 另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。   
  108.   
  109.     
  110.   
  111. stroke:描边   
  112.   
  113. android:width="2dp" 描边的宽度,android:color 描边的颜色。   
  114.   
  115. 我们还可以把描边弄成虚线的形式,设置方式为:   
  116.   
  117. android:dashWidth="5dp"  
  118.   
  119.     
  120.   
  121. android:dashGap="3dp"  
  122.   
  123. 其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。   
  124.   
  125.     
  126.   
  127. corners:圆角   
  128.   
  129. android:radius为角的弧度,值越大角越圆。   
  130.   
  131. 我们还可以把四个角设定成不同的角度,方法为:   
  132.   
  133. <corners     
  134.   
  135.         android:topRightRadius="20dp"    右上角     
  136.   
  137.         android:bottomLeftRadius="20dp"    右下角     
  138.   
  139.         android:topLeftRadius="1dp"    左上角     
  140.   
  141.         android:bottomRightRadius="0dp"    左下角     
  142.   
  143. />     
  144.   
  145.     
  146.   
  147. 这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。   
  148.   
  149. 还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。   
  150.   
  151.     
  152.   
  153. padding:间隔   
  154.   
  155. 这个就不用多说了,XML布局文件中经常用到。    
  156.   
  157.     
  158.   
  159. 具体代码如下:   
  160.   
  161.     
  162.   
  163. main.xml:     
  164.   
  165. <Button     
  166.   
  167.     android:layout_width="wrap_content"     
  168.   
  169.     android:layout_height="wrap_content"     
  170.   
  171.     android:text="TestShapeButton"     
  172.   
  173.     android:background="@drawable/button_selector"     
  174.   
  175.     />     
  176.   
  177. >     
  178.   
  179.     
  180.   
  181. button_selector.xml:   
  182.   
  183. <?xml version="1.0" encoding="utf-8"?>     
  184.   
  185. <selector     
  186.   
  187.     xmlns:android="http://schemas.android.com/apk/res/android">     
  188.   
  189.     <item android:state_pressed="true" >     
  190.   
  191.         <shape>     
  192.   
  193.             <!-- 渐变 -->     
  194.   
  195.             <gradient     
  196.   
  197.                 android:startColor="#ff8c00"     
  198.   
  199.                 android:endColor="#FFFFFF"     
  200.   
  201.                 android:type="radial"     
  202.   
  203.                 android:gradientRadius="50" />     
  204.   
  205.             <!-- 描边 -->     
  206.   
  207.             <stroke     
  208.   
  209.                 android:width="2dp"     
  210.   
  211.                 android:color="#dcdcdc"     
  212.   
  213.                 android:dashWidth="5dp"     
  214.   
  215.                 android:dashGap="3dp" />     
  216.   
  217.             <!-- 圆角 -->     
  218.   
  219.             <corners     
  220.   
  221.                 android:radius="2dp" />     
  222.   
  223.             <padding     
  224.   
  225.                 android:left="10dp"     
  226.   
  227.                 android:top="10dp"     
  228.   
  229.                 android:right="10dp"     
  230.   
  231.                 android:bottom="10dp" />     
  232.   
  233.         </shape>     
  234.   
  235.     </item>     
  236.   
  237.     <item android:state_focused="true" >     
  238.   
  239.         <shape>     
  240.   
  241.             <gradient     
  242.   
  243.                 android:startColor="#ffc2b7"     
  244.   
  245.                 android:endColor="#ffc2b7"     
  246.   
  247.                 android:angle="270" />     
  248.   
  249.             <stroke     
  250.   
  251.                 android:width="2dp"     
  252.   
  253.                 android:color="#dcdcdc" />     
  254.   
  255.             <corners     
  256.   
  257.                 android:radius="2dp" />     
  258.   
  259.             <padding     
  260.   
  261.                 android:left="10dp"     
  262.   
  263.                 android:top="10dp"     
  264.   
  265.                 android:right="10dp"     
  266.   
  267.                 android:bottom="10dp" />     
  268.   
  269.         </shape>     
  270.   
  271.     </item>     
  272.   
  273.     <item>           
  274.   
  275.         <shape>     
  276.   
  277.             <solid android:color="#ff9d77"/>     
  278.   
  279.             <stroke     
  280.   
  281.                 android:width="2dp"     
  282.   
  283.                 android:color="#fad3cf" />     
  284.   
  285.             <corners     
  286.   
  287.                 android:topRightRadius="5dp"     
  288.   
  289.                 android:bottomLeftRadius="5dp"     
  290.   
  291.                 android:topLeftRadius="0dp"     
  292.   
  293.                 android:bottomRightRadius="0dp"     
  294.   
  295.             />     
  296.   
  297.             <padding     
  298.   
  299.                 android:left="10dp"     
  300.   
  301.                 android:top="10dp"     
  302.   
  303.                 android:right="10dp"     
  304.   
  305.                 android:bottom="10dp" />     
  306.   
  307.         </shape>     
  308.   
  309.     </item>     
  310.   
  311. </selector>    


作者:KillerLegend
出处:http://www.cnblogs.com/KillerLegend/
分享最新的资源,分享个人所得,欢迎关注我的新浪微博
新浪微博主页:ikey4u
我的个人博客:www.ikey4u.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
原文地址:https://www.cnblogs.com/killerlegend/p/3352290.html