1)自定義button樣式
一、采用圖片方式
首先新建Android XML文件,類型選Drawable,根結點選selector,自定義一個文件名。
隨后,開發環境自動在新建的文件里加了selector結點,我們只需要在selector結點里寫上三種狀態時顯示的背景圖片(按下、獲取焦點,正常)即可。具體如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/play_press" ;/> <item android:state_focused="true" android:drawable="@drawable/play_press" ;/> <item android:drawable="@drawable/play" ;/> </selector>
注:這里獲取焦點跟點擊時顯示的是同一張圖片,必須嚴格照上面的順序寫,不可倒。
最后,只要在布局時寫Button控件時應用到Button的Background屬性即可,如:
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/button_style"> </Button>
二、采用自定義方式
在源代碼中,只需要修改button_style文件,同樣三種狀態分開定義:
<?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="#0d76e1"
android:endColor="#0d76e1" android:angle="270" /> <stroke android:width="1dip" android:color="#f403c9" /> <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="1dip" android:color="#f403c9" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#000000" android:endColor="#ffffff" android:angle="180" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="5dip" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
注:代碼中的各屬性含義為:
gradient 主體漸變
startColor開始顏色,endColor結束顏色 ,
angle開始漸變的角度(值只能為90的倍數,0時為左到右漸變,90時為下到上漸變,依次逆時針類推)
stroke 邊框 width 邊框寬度,color 邊框顏色
corners 圓角 radius 半徑,0為直角
padding text值的相對位置
2)自定義style樣式
一、在style.xml中自定義樣式
以自定義text文本大小和顏色為例,自定義一個名稱為"testStyle"的style代碼如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <style name="AppTheme" parent="AppBaseTheme"> </style> <style name="testStyle"> <item name="android:textSize">30px</item> <item name="android:textColor">#1110CC</item> <item name="android:width">150dip</item> <item name="android:height">150dip</item> </style> </resources>
二、在layout文件中引用自定義的"testStyle"的style樣式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView style="@style/testStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
從以上代碼可以看出,應用方式為@style/testStyle。