android中虚线的实现
这里我来分享下虚线的实现,话不多说,直接进入主题一.水平虚线在drawable下新建一个drawable资源文件dotted_line.xml,代码如下:<?xml version="1.0" encoding="utf-8"?><shapeandroid:shape="line" xmlns:android="http://schemas.android.com/apk/res
·
这里我来分享下虚线的实现,话不多说,直接进入主题
一.水平虚线
在drawable下新建一个drawable资源文件dotted_line.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="line" xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:dashWidth="2dp" //虚线的宽度
android:dashGap="2dp" //虚线间隔宽度
android:color="@color/theme_color" //虚线的颜色
android:width="1dp"/> //宽度
</shape>
然后在页面上使用
<TextView
android:background="@drawable/dotted_line"
android:layerType="software"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
二.垂直虚线
垂直虚线是在水平虚线的基础上进行一个90度的旋转就可得到,不过还是得做下处理。
再建一个drawable资源文件dotted_line_vertical.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-300dp"
android:right="-300dp">
<rotate
android:drawable="@drawable/dotted_line" //引用水平虚线的drawable
android:fromDegrees="90"/> //旋转90度
</item>
</layer-list>
记住,这里一定得设置 left和 right属性,如果不加会显示不出来
在页面上使用和水平一样通过background来使用,如下:
<TextView
android:background="@drawable/dotted_line_vertical"
android:layout_width="1dp"
android:layout_height="300dp"/>
更多推荐
已为社区贡献15条内容
所有评论(0)