Android 實現虛線,實線
- 虛線
- 實線
虛線
實現的方法是比較簡單的,不多說,直接看代碼.
<?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:dashWidth="10dp"
android:color="#f84d4d"
android:dashGap="4dp"/>
<!--這裏是虛線的高度-->
<size android:height="1dp"/>
</shape>
這裏說明一下:
- dashWith 是虛線的長度
- dashGap 是虛線之間的寬度
- color 是虛線的顏色
但是,在實時預覽裏面能看到虛線了,但是程序跑起來之後,發現仍然是實線.這裏還要在清單文件中,把硬件加速給禁掉.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="false" >
- android:hardwareAccelerated=”false” 硬件加速設爲false
這樣就實現了從xml中實現虛線的方法.
實線
這個是比較簡單的.根據上面的虛線來設置就行.
<stroke
android:width="1dp"
android:color="#f84d4d"/>
<size android:height="1dp"/>
虛線和實線的區別就是有沒有dashGap.
這樣也就簡單的實現了實線的編寫.