自定义drawable(shape)

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

 

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#f033ff22"/> <stroke android:width="2dp" android:color="#ff1111" android:dashWidth="2dp" android:dashGap="0dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> <corners android:radius="6dp" /> </shape>

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

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

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

corners定义四角圆弧的半径。

 

TIP:

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

 

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="false" android:drawable="@drawable/gallery_selected_unfocused" /> <item android:state_focused="true" android:drawable="@drawable/gallery_selected_focused" /> <item android:state_pressed="true" android:drawable="@drawable/gallery_unselected_focused" /> <item android:state_selected="true" android:drawable="@drawable/gallery_unselected_focused" /> </selector>

 

--->>><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="false" > <shape> <solid ...> </solid> </shape> </item> <item android:state_focused="true" > <shape> <solid ...> </solid> </shape> </item> <item android:state_pressed="true" > <shape> <solid ...> </solid> </shape> </item> </selector>

你可能感兴趣的:(android,encoding)