在android開發(fā)中 ,我們需要通過(guò)drawable圖片來(lái)改變控件的背景或者樣式。如果我們沒有呢,就需要我們?cè)诳丶羞M(jìn)行自定義控件樣式。
自定義圖形shape,Android上支持以下幾種屬性shape、gradient、stroke、corners、padding、solid等
<?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"
/>
<!-- 填充 -->
<solid
android:color="@android:color/white"/><!-- 填充的顏色 -->
<!-- 描邊 -->
<stroke
android:width="2dp"
android:color="@android:color/holo_red_light"
android:dashWidth="5dp"
/>
<!-- 漸變 -->
<gradient
android:startColor="@android:color/white"
android:centerColor="@android:color/holo_blue_dark"
android:endColor="@android:color/holo_green_dark"
android:useLevel="true"
android:angle="45"
android:type="linear"
android:centerX="5"
android:centerY="6"
android:gradientRadius="90"/>
<!-- 間隔 -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/><!-- 各方向的間隔 -->
<!-- 大小 -->
<size
android:width="50dp"
android:height="50dp"/><!-- 寬度和高度 -->
</shape>
填充:設(shè)置填充的顏色
間隔:設(shè)置四個(gè)方向上的間隔
大?。涸O(shè)置大小
圓角:同時(shí)設(shè)置五個(gè)屬性,則Radius屬性無(wú)效
android:Radius="2dp" 設(shè)置四個(gè)角的半徑
android:topLeftRadius="2dp" 設(shè)置左上角的半徑
android:topRightRadius="2dp" 設(shè)置右上角的半徑
android:bottomLeftRadius="2dp" 設(shè)置右下角的半徑
android:bottomRightRadius="2dp" 設(shè)置左下角的半徑
描邊:dashWidth和dashGap屬性,只要其中一個(gè)設(shè)置為0dp,則邊框?yàn)閷?shí)現(xiàn)邊框
android:width="2dp" 設(shè)置邊邊的寬度
android:color="@android:color/white" 設(shè)置邊邊的顏色
android:dashWidth="2dp" 設(shè)置虛線的寬度
android:dashGap="2dp" 設(shè)置虛線的間隔寬度
漸變:android:startColor和android:endColor分別為起始和結(jié)束顏色,
ndroid:angle是漸變角度,必須為45的整數(shù)倍。
另外漸變默認(rèn)的模式為android:type="linear",即線性漸變,可以指定漸變?yōu)閺较驖u變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。