以建立一個灰色的帶圓角邊框的Button爲例:
1、建立一個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官方文檔:
2、在Button中應用ShapeDrawable
main.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等。