最近开发产品遇到一个要求虚线的,上网查了下,can use shape ,好牛呗。。。下面说下怎么用,和一些bug
画虚线的shape (hidden_line.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<!-- 破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线 -->
<stroke
android:dashGap="3dp"
android:dashWidth="6dp"
android:width="1dp"
android:color="@color/line_gray" />
<!-- 虚线的高度 -->
<size android:height="1dp" />
</shape>
在布局中使用:
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layerType="software"
android:layout_marginTop="10dp"
android:background="@drawable/hidden_line"
/>
(1)view的layout_height的宽度要比虚线的宽度大
(2)android:layerType="software" 这个如果不加的话,你会发现在视图中看着没问题,当时在手机上还是一条直线。。why
because 从android3.0(也有可能是4.0)开始,安卓关闭了硬件加速功能,所以就不能显示了.除了这种方式还有另外两种,不过感觉应该应该没人用
a. view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
b. android:hardwareAccelerated="false"