当前位置: 代码迷 >> Android >> 50个Android开发技艺(12 为控件添加圆角边框)
  详细解决方案

50个Android开发技艺(12 为控件添加圆角边框)

热度:20   发布时间:2016-04-28 05:57:09.0
50个Android开发技巧(12 为控件添加圆角边框)
控件的圆角边框可以使你的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中应用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等。



  相关解决方案