Android中自定義漂亮動態樣式
下面給大家演示制作工款精美的控件樣式,可以應用到多種控件上。
1、在res中新建個drawable文件夾,然后在這文件夾上右擊選擇“新建”-“其它”-“Android Xml File”-“下一步”-輸入文件名“my_style”,然后在下面選擇“selector”。然后打開這個Xml文件,里面添加為如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape> <corners android:radius="10px"/> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f"></gradient> <stroke android:width="2px" android:color="#ff0"></stroke> </shape> </item> <item android:state_active="true"> <shape> <corners android:radius="5px"/> <gradient android:startColor="#000" android:centerColor="#0f0" android:endColor="#fff"></gradient> <stroke android:width="2px" android:color="#f0f"></stroke> </shape> </item> </selector>
說明:紛色字體內容為您手動添加的。第一個item的內容表示默認樣式,第二個item由於標注了:android:state_active="true",所以表示激活狀態下的樣式。根局自己的愛好,可以手動設計更多的樣式,更多的item。
2、在layout布局Xml中,給對應控件添加樣式:以TimePicker為例,添加 android:background="@drawable/my_style" /> 個:
<TimePicker android:id="@+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="41dp" android:background="@drawable/my_style" />
當然了,給按鈕也呆以添加 android:background="@drawable/my_style" 來應用樣式。
3、效果如下:

。。。好了,發揮你的才智吧。我這里只是拋磚引玉,思路就是這樣,大家好好發揮。
本文來自宋興柱博客員:http://www.cnblogs.com/songxingzhu/
