1)自定義button樣式android
1、採用圖片方式 佈局
首先新建Android XML文件,類型選Drawable,根結點選selector,自定義一個文件名。spa
隨後,開發環境自動在新建的文件里加了selector結點,咱們只須要在selector結點裏寫上三種狀態時顯示的背景圖片(按下、獲取焦點,正常)便可。具體以下:code
<?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屬性便可,如:xml
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/button_style"> </Button>
2、採用自定義方式blog
在源代碼中,只須要修改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>
注:代碼中的各屬性含義爲:ip
gradient 主體漸變utf-8
startColor開始顏色,endColor結束顏色 ,開發
angle開始漸變的角度(值只能爲90的倍數,0時爲左到右漸變,90時爲下到上漸變,依次逆時針類推)
stroke 邊框 width 邊框寬度,color 邊框顏色
corners 圓角 radius 半徑,0爲直角
padding text值的相對位置
2)自定義style樣式
1、在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>
2、在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。