標籤:角度 背景圖 resources version 半徑 str sources match size
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。
android中樣式和自訂button樣式