找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 11968|回復(fù): 0
收起左側(cè)

關(guān)于在AndroidStudio中自定義Button的樣式

[復(fù)制鏈接]
ID:223768 發(fā)表于 2017-8-1 10:52 | 顯示全部樓層 |閱讀模式
于自定義Button的樣式,是在res/drawable文件夾下自定義一個xml文件,在xml文件中進行定義,然后通過將Button的backgroud改為定義的xml文件實現(xiàn)的。下面將進行詳細介紹,并舉例說明。


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

[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>

有很多樣式可夠選擇:

在使用時,

  • <?xml version="1.0" encoding="utf-8"?>  
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  •     android:layout_width="fill_parent"  
  •     android:layout_height="fill_parent"  
  •     android:orientation="vertical" >  
  •   
  •     <TextView  
  •         android:layout_width="fill_parent"  
  •         android:layout_height="wrap_content"  
  •         android:text="@string/hello" />  
  •   
  •     <Button  
  •         android:id="@+id/button1"  
  •         android:layout_width="wrap_content"  
  •         android:layout_height="wrap_content"  
  •         android:background="@drawable/button_style"  
  •         />  
  •   
  • </LinearLayout>
這樣就完成了簡單的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>  
  • 感謝大家觀看!


回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表