1. 程式人生 > >android通過shape實現虛線效果

android通過shape實現虛線效果

shape資源

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:dashGap="5dp"
        android:dashWidth="8dp"
        android:width="1dp"
        android:color="#70ffffff" />
    <!-- 虛線的高度 -->
    <size android:height="1dp" />
</shape>

layout檔案設定background即可:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layerType="software"
    android:background="@drawable/line_dash" />

有的裝置會顯示為實線,原因是4.0以上預設把Activity的硬體加速打開了,所以我們在Manifest.xml中關掉Activity的android:hardwareAccelerated="false"
不過不推薦這樣實現,還是上面View層實現比較好。因為硬體加速有時候還是必要的!