控制項的圓角邊框可以使你的App看起來更美觀,其實實現起來也很簡單。
(原文地址:http://blog.csdn.net/vector_yi/article/details/24463025)
以建立一個灰色的帶圓角邊框的Button為例:
一、建立一個ShapeDrawable作為背景在drawable目錄下建立一個button_rounded_background.xml檔案:
<shape xmlns:android = "http://schemas.android.com/apk/res/android" android:shape= "rectangle" > <solid android:color= "#AAAAAA" /> <corners android:radius= "15dp" /></shape>
見名知意,除了<solid/>,<corners/>標籤外,<shape/>還支援很多不同功能標籤,更多介紹請移步Android官方文檔:http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
二、在Button中應用ShapeDrawablemain.xml:
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:gravity= "center" > <Button android:id ="@+id/button" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:background ="@drawable/button_rounded_background" android:padding ="10dp" android:text ="@string/hello" android:textColor ="#000000" /></RelativeLayout>
至此就已經構建完成了一個帶圓角邊框的Button。
ShapeDrawable不僅可以利用在Button中,它還可以應用與所有帶背景的控制項,例如ListView中的Item等。