50个Android开发技巧(12 为控件添加圆角边框)
2015-11-24 00:00
489 查看
控件的圆角边框可以使你的App看起来更美观,其实实现起来也很简单。
(原文地址:http://blog.csdn.net/vector_yi/article/details/24463025)
以创建一个灰色的带圆角边框的Button为例:
一、创建一个ShapeDrawable作为背景
在drawable目录下创建一个
button_rounded_background.xml文件:
[html]
view plain
copy
<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:
[html]
view plain
copy
<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等。
(原文地址:http://blog.csdn.net/vector_yi/article/details/24463025)
以创建一个灰色的带圆角边框的Button为例:
一、创建一个ShapeDrawable作为背景
在drawable目录下创建一个
button_rounded_background.xml文件:
[html]
view plain
copy
<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:
[html]
view plain
copy
<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等。
相关文章推荐
- 50个Android开发技巧(02 延迟加载和避免重复渲染视图)
- 根据屏幕的宽度,动态设置控件高度
- Android studio的tips--布局预览
- android菜单插件Slidingmenu和Actionbarsherlock组合使用做侧滑菜单
- Android Scrollview+Listview 实现不同条目点赞并计数功能及原理分析(3)
- Ubuntu 下载 & 编译 Android5.1 源码
- Android开源DiscreteSeekbar:动画气泡指示当前滑动值
- Android.mk的用法和基础
- Android基础学-1
- android之ListPreference的用法_PreferenceActivity用法
- Android Studio 实用技巧
- Activity.createPendingResult() android组件间通信又一种方式
- 【NDK开发】android-ndk r10环境搭建
- 源码BaseAdapter浅谈
- 优秀博客_Android
- 我的Android我做主
- Android Activity启动源码详解
- Android开源项目第五篇——优秀个人和团体篇
- android datepicker源码
- 自定义地图数据瓦片化请求的一种实现方案