自定义drawable(shape)

来源:互联网 发布:java权限管理的实现 编辑:程序博客网 时间:2024/06/06 02:33

原文地址:点击打开链接

android中可以通过shape对drawable进行自定义。

 

[xhtml] view plaincopy
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">  
  2.     <solid android:color="#f033ff22"/>  
  3.     <stroke android:width="2dp" android:color="#ff1111" android:dashWidth="2dp"  
  4.      android:dashGap="0dp" />  
  5.     <padding android:left="10dp" android:top="10dp"  
  6.             android:right="10dp" android:bottom="10dp" />  
  7.     <corners android:radius="6dp" />  
  8. </shape>   

solid中定义的颜色值需要包含alpha值,

stroke可以定义边框的颜色,width为边框的宽度,dashWidth为画笔的宽度,dashGap为画笔的间距

(dashGap为0,则边框为实心的边线)

corners定义四角圆弧的半径。

 

TIP:

对于使用selector定义不同的状态也可以使用shape替代drawable的属性。

 

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">     
  3.     <item android:state_focused="false"     
  4.         android:drawable="@drawable/gallery_selected_unfocused" />     
  5.     <item android:state_focused="true"     
  6.         android:drawable="@drawable/gallery_selected_focused" />     
  7.     <item android:state_pressed="true"     
  8.         android:drawable="@drawable/gallery_unselected_focused" />     
  9.    <item android:state_selected="true"     
  10.         android:drawable="@drawable/gallery_unselected_focused" />     
  11. </selector>  
[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">     
  3.     <item android:state_focused="false" >  
  4.         <shape>  

0 0