Android通过代码实现虚线或者虚线框shape

时间:2022-04-02 03:52:35

虚线

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">

<stroke
android:width="1dp"
android:color="#f3f3f3"
android:dashWidth="3dp"
android:dashGap="3dp" />

</shape>

虚线框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
android:width="1dp"
android:color="#f3f3f3"
android:dashWidth="3dp"
android:dashGap="3dp" />

</shape>

虚线圆

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<stroke
android:width="1dp"
android:color="#f3f3f3"
android:dashWidth="3dp"
android:dashGap="3dp" />

</shape>

顶部虚线

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-2dp"
android:right="-2dp"
android:bottom="-2dp">

<shape>
<stroke
android:width="1dp"
android:color="#f3f3f3"
android:dashWidth="3dp"
android:dashGap="3dp" />

</shape>
</item>
</layer-list>

底部虚线

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">

<shape>
<stroke
android:width="1dp"
android:color="#f3f3f3"
android:dashWidth="3dp"
android:dashGap="3dp" />

</shape>
</item>
</layer-list>

使用

android:background="@drawable/shape_dash_line"