(注:以前不知道這一點,還鬧個笑話,在Timers4Me中自定義TimePicker控件的時候,下載下傳了相應的源碼和資源,看到它的圖檔,我還很納悶,為什麼最後多了個.9,并且圖檔還有幾條黑線幾個點,還特意讓美工對圖檔做了處理,将多餘的部分去掉。)
本例中,用到以下圖檔:
* 按鈕可以點選,但處于沒有點選狀态下的黑色XX.9格式背景圖檔
<a target="_blank" href="http://blog.51cto.com/attachment/201103/194227210.png"></a>
* 按鈕可以點選,被點選下的狀态的橙色的XX.9格式背景圖檔
<a target="_blank" href="http://blog.51cto.com/attachment/201103/195256371.png"></a>
* 按鈕不可用狀态下的XX.9格式的背景圖檔
<a target="_blank" href="http://blog.51cto.com/attachment/201103/195401799.png"></a>
上面的三張圖檔,需要放到項目中/res/drawables高中低密度中任何一個檔案夾下
2.下面我們需要定義按鈕處于不同狀态下的樣式:在/res/drawables/目錄下高中低任何一個檔案夾下,建立xml檔案,名字可以自己取,這裡我命名為custom_button.xml,在不同的狀态下會引用相應的圖檔:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/btn_background_red" />
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="@drawable/btn_background_orange" />
<item android:state_focused="true" android:state_enabled="true"
<item android:state_enabled="true" android:drawable="@drawable/btn_background_black" />
</selector>
這裡定義還是要按照規定的順序來的,如果把最後一項放到第一條,那麼隻要按鈕可用狀态下,第一條條件成立,下面的就不會執行了。
3.另外還需要在/res/values/中建立一個xml檔案,名稱為styles.xml,這裡定義了按鈕的文本樣式及不同狀态下的樣式:
<resources>
<style name="CustomButton" parent="@android:style/Widget.Button">
<!-- 水準、垂直居中 -->
<item name="android:gravity">center_vertical|center_horizontal</item>
<!-- 字型顔色 -->
<item name="android:textColor">#FFFFFFFF</item>
<!-- 指定文本陰影的顔色,需要與shadowRadius一起使用 -->
<item name="android:shadowColor">#FF000000</item>
<!-- 設定陰影橫向坐标開始的位置 -->
<item name="android:shadowDx">0</item>
<!-- 設定陰影縱向坐标開始的位置 -->
<item name="android:shadowDy">-1</item>
<!-- 設定陰影的半徑,設定為0.1就變成文本的顔色了 -->
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<!-- 這裡引用定義的按鈕樣式 -->
<item name="android:background">@drawable/custom_button</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
</resources>
4.這裡使用了theme,這種定義可以是程式中所有的按鈕都是這樣的外觀,當然了,在實際項目中,不大可能會有這樣的需求,隻是體會這樣使用方式而已,在/res/values/目錄中建立xml檔案,命名為themes.xml檔案:
<!-- 自定義主題:沒有标題欄,規定按鈕的樣式為CustomButton中定義的 -->
<style name="CustomTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">@style/CustomButton</item>
5.對主題的定義,需要在AndroidManifest.xml的application節點中引用相應的主題,由于主題中規定了按鈕的樣式,是以所有的按鈕都自然成了定義的外觀。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobileares.widget.custombutton" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<!-- 使用主題 -->
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@style/CustomTheme">
<activity android:name=".CustomButton" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
本文轉自 breezy_yuan 51CTO部落格,原文連結:http://blog.51cto.com/lbrant/504645,如需轉載請自行聯系原作者