Android虚线制作

作者: 喂_balabala | 来源:发表于2017-12-27 11:45 被阅读23次

    在drawable目录自定义xml进行绘制

      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
      3.      android:shape="line">  
      4.     <stroke  
      5.         android:width="1dp"  
      6.         android:color="@color/white"  
      7.         android:dashWidth="5dp"  
      8.         android:dashGap="2dp" />  
      9. </shape>  
    

    然后在需要画虚线的地方使用该drawable作为背景即可。
    不过如果需要一条竖虚线,就麻烦很多。
    首先,同样定义xml文件,不过要旋转90度,这样就是竖的了:

      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:fromDegrees="90"  
      4.     android:toDegrees="90">  
      5.     <shape android:shape="line">  
      6.         <stroke  
      7.             android:width="1dp"  
      8.             android:color="@color/white"  
      9.             android:dashWidth="5dp"  
      10.             android:dashGap="2dp"  
      11.             />  
      12.     </shape>  
      13. </rotate>  
    

    另外,在使用该drawable时,宽度不能设为1dp,因为这个宽度是旋转前的虚线长度,如果设为1dp,则看不出虚线了,所以需要一点小技巧:
    1)在view的宽度设大一些,然后设置marginLeft 和marginRight 为负值,就不会影响到旁边的view了

      1. <View  
      2.     android:background="@drawable/dot_line_white"  
      3.     android:layout_marginLeft="-10dp"  
      4.     android:layout_marginRight="-10dp"  
      5.     android:layerType="software"  
      6.     android:layout_width="50dp"  
      7.     android:layout_height="match_parent"/>  
    

    2)使用FrameLayout等布局方式,将虚线view置于其他view之上。
    注意:设置时必须设置layerType为software,否则手机显示不会显示出虚线。

    相关文章

      网友评论

        本文标题:Android虚线制作

        本文链接:https://www.haomeiwen.com/subject/uptugxtx.html