Android利用shape画虚线
2016-01-12 15:17
615 查看
我们知道,想在Android XML中画出一条直线,很简单:
如果想要画出一条虚线呢?
在drawable目录下新建bg_dash_line.xml
说明:
显示一条虚线,width为线条的高度,dashWidth为破折线的宽度,dashGap为破折线之间的空隙的宽度,当dashGap=0时,就是实线
注意:
1. 如果在<stroke>标签中设置了android:width,则在<View>标签中android:layout_height的值必须大于android:width的值,否则虚线不会显示。如果不设置,默认android:width为0。
2. 关于4.0以上设备虚线会变实线的问题:
代码中可以添加:
[java] view
plaincopy
line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
XML中可以添加:
[html] view
plaincopy
android:layerType="software"
如上例所示,如果想正常的显示虚线:
<View android:layout_width="match_parent" android:layout_height="1px" android:background="#FFFFFF"/>
如果想要画出一条虚线呢?
在drawable目录下新建bg_dash_line.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="3px" android:color="#FFFFFF" android:dashWidth="10px" android:dashGap="10px" /> </shape>
说明:
显示一条虚线,width为线条的高度,dashWidth为破折线的宽度,dashGap为破折线之间的空隙的宽度,当dashGap=0时,就是实线
注意:
1. 如果在<stroke>标签中设置了android:width,则在<View>标签中android:layout_height的值必须大于android:width的值,否则虚线不会显示。如果不设置,默认android:width为0。
2. 关于4.0以上设备虚线会变实线的问题:
代码中可以添加:
[java] view
plaincopy
line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
XML中可以添加:
[html] view
plaincopy
android:layerType="software"
如上例所示,如果想正常的显示虚线:
<View android:layout_width="match_parent" android:layout_height="4px" android:layerType="software" android:background="@drawable/bg_dash_line"/>
相关文章推荐
- Android BLE蓝牙4.0开发详解
- android 自定义view(二)
- Android安全研究经验谈
- Android安全研究经验谈
- android中popupwindow的点滴
- 安卓开发之百度地图定位
- Fresco介绍 - 一个新的android图片加载库
- android选择图片拍照详解(裁剪-旋转-压缩)
- Android 判断一个url是否有效
- Android如何在java代码中设置margin
- Android Studio上NDK/JNI开发环境问题
- Android属性动画完全解析(三)-Interpolator和ViewPropertyAnimator的用法
- android studio 运行出现 com.android.ide.common.process.ProcessException
- Android 监听输入法隐藏与显示状态切换
- [Andorid开发艺术探索 读书笔记]View的事件体系(二)
- 解决更新Android SDK时出现A folder failed to be renamed or moved办法
- Android中定时执行任务的3种实现方法
- Android Studio 快捷键
- Android studio快捷键
- 实现android M 的直接分享