50個Android開發技巧(12 爲控件添加圓角邊框)

控件的圓角邊框能夠使你的App看起來更美觀,其實實現起來也很簡單。
(原文地址:http://blog.csdn.net/vector_yi/article/details/24463025)
以建立一個灰色的帶圓角邊框的Button爲例:


1、建立一個ShapeDrawable做爲背景
在drawable目錄下建立一個 button_rounded_background.xml文件:
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <shape xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:shape"rectangle" >  
  3.     <solid android:color"#AAAAAA" />  
  4.     <corners android:radius"15dp" />  
  5. </shape>  
見名知意,除了<solid/>,<corners/>標籤外,<shape/>還支持不少不一樣功能標籤,更多介紹請移步Android官方文檔:

2、在Button中應用ShapeDrawable
main.xml:
[html]   view plain copy 在CODE上查看代碼片 派生到個人代碼片
  1. <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"fill_parent"  
  3.     android:layout_height"fill_parent"  
  4.     android:gravity"center" >  
  5.   
  6.     <Button  
  7.         android:id ="@+id/button"  
  8.         android:layout_width ="wrap_content"  
  9.         android:layout_height ="wrap_content"  
  10.         android:background ="@drawable/button_rounded_background"  
  11.         android:padding ="10dp"  
  12.         android:text ="@string/hello"  
  13.         android:textColor ="#000000" />  
  14.   
  15. </RelativeLayout>  
至此就已經構建完成了一個帶圓角邊框的Button。

ShapeDrawable不只能夠利用在Button中,它還能夠應用與全部帶背景的控件,例如ListView中的Item等。
相關文章
相關標籤/搜索