歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android自定義按鈕樣式

Android自定義按鈕樣式

日期:2017/3/1 10:08:53   编辑:Linux編程

使得按鈕在不同的狀態有不同的背景圖片是本篇的主要類容

在res/drawable下新建一個buttonstyle.xml文件,這個文件用於描述按鈕的樣式

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:Android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_pressed="true" android:drawable="@drawable/btn_p"/>
  4. <item android:state_pressed="false" android:drawable="@drawable/btn_n"/>
  5. </selector>

還有很多的樣式如下圖

在布局文件中添加一個Button,使用buttonstyle.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <Button
  11. android:id="@+id/button1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:background="@drawable/buttonstyle"
  15. />
  16. </LinearLayout>

這樣就完成了Button的樣式。

Copyright © Linux教程網 All Rights Reserved