Button按钮自定义样式
生活随笔
收集整理的這篇文章主要介紹了
Button按钮自定义样式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? 在進(jìn)行UI設(shè)計(jì)時(shí)為了美觀,可以自定義Button按鈕的外觀。比如自定義圖片作為按鈕的背景,直接將圖片復(fù)制到項(xiàng)目的res/drawable/路徑下,然后在xml文件中使用android:background進(jìn)行定義即可。若想要在不同的狀態(tài)下呈現(xiàn)不同的背景樣式,則處理方法如下:
(1)??????在res/drawable/?目錄下新建一個(gè)xml文件命名為android_button.xml.
(2)??????在該文件中編寫如下代碼:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/android_pressed"android:state_pressed="true" /><item android:drawable="@drawable/android_focused"android:state_focused="true" /><item android:drawable="@drawable/android_normal" /> </selector>這就相當(dāng)于定義了一個(gè)單獨(dú)的樣式文件,每個(gè)item中包含按鈕的背景圖片和所處的狀態(tài),根據(jù)狀態(tài)按鈕呈現(xiàn)出不同的背景。當(dāng)按鈕被按下,也就是state_pressed為true時(shí)按鈕的背景圖片為drawable文件夾下的android_pressed.png文件。
(3)??????在使用Button的Activity所對(duì)應(yīng)的xml文件中,使用android:background聲明該樣式,指定使用drawable文件夾下的android_button.xml文件。
<Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:background="@drawable/android_button"/>?
總結(jié)
以上是生活随笔為你收集整理的Button按钮自定义样式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转载]打工辛酸路:我是一朵飘零的花之1
- 下一篇: 【我的创作一周年纪念日】爱情是需要被纪念