项目中某些布局可能有这样的需求,画一条虚线作为分割线
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,不然显示出来的是实线。
android shape的应用之画一条虚线
最新推荐文章于 2021-07-19 13:43:29 发布