Button按钮自定义样式
生活随笔
收集整理的這篇文章主要介紹了
Button按钮自定义样式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 在進行UI設計時為了美觀,可以自定義Button按鈕的外觀。比如自定義圖片作為按鈕的背景,直接將圖片復制到項目的res/drawable/路徑下,然后在xml文件中使用android:background進行定義即可。若想要在不同的狀態下呈現不同的背景樣式,則處理方法如下:
(1)??????在res/drawable/?目錄下新建一個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>這就相當于定義了一個單獨的樣式文件,每個item中包含按鈕的背景圖片和所處的狀態,根據狀態按鈕呈現出不同的背景。當按鈕被按下,也就是state_pressed為true時按鈕的背景圖片為drawable文件夾下的android_pressed.png文件。
(3)??????在使用Button的Activity所對應的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"/>?
總結
以上是生活随笔為你收集整理的Button按钮自定义样式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转载]打工辛酸路:我是一朵飘零的花之1
- 下一篇: 【我的创作一周年纪念日】爱情是需要被纪念