控件的圆角边框能够使你的App看起来更美观,事实上实现起来也非常easy。

(原文地址:http://blog.csdn.net/vector_yi/article/details/24463025)

以创建一个灰色的带圆角边框的Button为例:

50个Android开发技巧(12 为控件加入圆角边框)-LMLPHP

一、创建一个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官方文档:
二、在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等。

05-11 09:21