剛剛翻論壇的時候看到的,總覺得以後會用的上這些零零碎碎的東西,所以轉載流存啦~~~~
原文http://www.eoeandroid.com/thread-71143-1-1.html
大家都應該明白的,在Android中常常會使用shape來定義控制項的一些顯示內容,那麼怎麼來用那,今天我們就來看一些shape的使用,大家看完這篇以後就會對shape有了大體的瞭解,下面的是稍作總結請大家仔細的觀看:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:usesLevel=["true" | "false"] /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> </shape>
Java代碼:
- <shape>
- <!-- 實心 -->
- <solid android:color="#ff9d77"/>
- <!-- 漸層 -->
- <gradient
- android:startColor="#ff8c00"
- android:endColor="#FFFFFF"
- android:angle="270" />
- <!-- 描邊 -->
- <stroke
- android:width="2dp"
- android:color="#dcdcdc" />
- <!-- 圓角 -->
- <corners
- android:radius="2dp" />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
複製代碼
solid:實心,就是填充的意思
android:color指定填充的顏色
gradient:漸層
android:startColor和android:endColor分別為起始和結束顏色,ndroid:angle是漸層角度,必須為45的整數倍。
另外漸層預設的模式為android:type="linear",即線性漸層,可以指定漸層為放射狀漸層,android:type="radial",放射狀漸層需要指定半徑android:gradientRadius="50"。
stroke:描邊
android:width="2dp" 描邊的寬度,android:color 描邊的顏色。
我們還可以把描邊弄成虛線的形式,設定方式為:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。
corners:圓角
android:radius為角的弧度,值越大角越圓。
我們還可以把四個角設定成不同的角度,方法為:
Java代碼:
- <corners
- android:topRightRadius="20dp" 右上方
- android:bottomLeftRadius="20dp" 右下角
- android:topLeftRadius="1dp" 左上方
- android:bottomRightRadius="0dp" 左下角
- />
複製代碼
這裡有個地方需要注意,bottomLeftRadius是右下角,而不是左下角,這個有點鬱悶,不過不影響使用,記得別搞錯了就行。
還有網上看到有人說設定成0dp無效,不過我在測試中發現是可以的,我用的是2.2,可能修複了這個問題吧,如果無效的話那就只能設成1dp了。
padding:間隔
這個就不用多說了,XML布局檔案中經常用到。
大體的就是這樣,以下是一個使用的具體樣本:用在Selector中作為Button的背景,分別定義了按鈕的一般狀態、獲得焦點狀態和按下時的狀態,具體代碼如下:
Java代碼:
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TestShapeButton"
- android:background="@drawable/button_selector"
- />
複製代碼
我們來看看main.xml代碼:
Java代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" >
- <shape>
- <!-- 漸層 -->
- <gradient
- android:startColor="#ff8c00"
- android:endColor="#FFFFFF"
- android:type="radial"
- android:gradientRadius="50" />
- <!-- 描邊 -->
- <stroke
- android:width="2dp"
- android:color="#dcdcdc"
- android:dashWidth="5dp"
- android:dashGap="3dp" />
- <!-- 圓角 -->
- <corners
- android:radius="2dp" />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- <item android:state_focused="true" >
- <shape>
- <gradient
- android:startColor="#ffc2b7"
- android:endColor="#ffc2b7"
- android:angle="270" />
- <stroke
- android:width="2dp"
- android:color="#dcdcdc" />
- <corners
- android:radius="2dp" />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- <item>
- <shape>
- <solid android:color="#ff9d77"/>
- <stroke
- android:width="2dp"
- android:color="#fad3cf" />
- <corners
- android:topRightRadius="5dp"
- android:bottomLeftRadius="5dp"
- android:topLeftRadius="0dp"
- android:bottomRightRadius="0dp"
- />
- <padding
- android:left="10dp"
- android:top="10dp"
- android:right="10dp"
- android:bottom="10dp" />
- </shape>
- </item>
- </selector>
複製代碼
運行效果:
昨天 09:21 上傳
下載附件(8.53 KB)
昨天 09:21 上傳
下載附件(8.55 KB)