android shape的应用之画一条虚线

项目中某些布局可能有这样的需求,画一条虚线作为分割线

1.先写一个shape,命名为shape_line_dash.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:width="@dimen/dp1"
          android:color="#f00"
          android:dashGap="@dimen/dp2"
          android:dashWidth="@dimen/dp4"/>
      </shape>

2.再在布局文件中写一个View,引用第一步中的shape
      <View
      android:layerType="software"
      android:layout_width="match_parent"
      android:layout_height="@dimen/dp2"
      android:layout_marginLeft="@dimen/dp17"
      android:layout_marginRight="@dimen/dp17"
      android:background="@drawable/shape_line_dash"/>

注意: 

  1.引用的时候,View的height要大于shape中stroke的width,不然会显示不出来,或者显示不全。

  2.还要设置一个属性layerType为software,不然显示出来的是实线。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值