標(biāo)題: 關(guān)于在AndroidStudio中自定義Button的樣式 [打印本頁]

作者: 78701979    時(shí)間: 2017-8-1 10:52
標(biāo)題: 關(guān)于在AndroidStudio中自定義Button的樣式
于自定義Button的樣式,是在res/drawable文件夾下自定義一個(gè)xml文件,在xml文件中進(jìn)行定義,然后通過將Button的backgroud改為定義的xml文件實(shí)現(xiàn)的。下面將進(jìn)行詳細(xì)介紹,并舉例說明。


在res/drawable下新建一個(gè)button_style.xml文件,這個(gè)文件用于描述按鈕的樣式

[html] view plain copy



  • <?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/btn_p"/>  
  •     <item android:state_pressed="false" android:drawable="@drawable/btn_n"/>  
  • </selector>

有很多樣式可夠選擇:

在使用時(shí),

這樣就完成了簡(jiǎn)單的Button樣式。
可以利用上述的參數(shù)設(shè)置完成一些很酷炫的按鈕樣式。

[html] view plain copy


  • <?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="#99CC33"  
  •                 android:endColor="#99CC33"  
  •                 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="#336633"  
  •                 android:endColor="#336633"  
  •                 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>  
  • 感謝大家觀看!







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1