編輯:Android 程式設計入門
先看效果,再談做法。
點擊效果:
在 res/drawable 下建立一個 XML:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:dashGap="3dp"
android:dashWidth="6dp"
android:width="1dp"
android:color="#63a219" />
<!-- 虛線的高度 -->
<size android:height="1dp" />
</shape>
三個關鍵屬性:
dashWidth:每一段實線的寬度。dashGap:實線之間空隙的寬度。width:線條本身的粗細。當 dashGap="0dp" 時,就退化成實線了。另外 android:shape="line" 時,size 的高度是必要的,否則可能完全不顯示。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充顏色 -->
<solid android:color="#FFFFFF"></solid>
<!-- 線的寬度與顏色 -->
<stroke android:width="1dp" android:color="#63a219"></stroke>
<!-- 矩形的圓角半徑 -->
<corners android:radius="10dp" />
</shape>
最簡單的用法就是 <corners android:radius="10dp" />,四個角一致。
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 分別對應左上、右上、左下、右下的圓角半徑 -->
<corners
android:topLeftRadius="0dp"
android:topRightRadius="7dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="7dp"/>
<!-- 設置漸層 -->
<gradient
android:startColor="#9cff00"
android:endColor="#197600"
android:angle="270"/>
<stroke
android:width="1dp"
android:color="#63a219" />
</shape>
關於 angle:它必須是 45 的倍數。0 是由左到右,90 是由下到上,270 是由上到下。這裡寫 270 就是常見的「上淺下深」效果。搞錯方向是新手最常踩的坑。
先做一個 style 讓版面引用比較乾淨:
<style name="list_item_top">
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:paddingTop">10dip</item>
<item name="android:paddingBottom">10dip</item>
<item name="android:paddingLeft">10dip</item>
<item name="android:paddingRight">10dip</item>
<item name="android:gravity">center_vertical</item>
<item name="android:background">@drawable/background_view_rounded_top</item>
</style>
接著用 selector 搭配 pressed 狀態,做出按下時換色的效果:
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="https://schemas.android.com/apk/res/android"
android:insetLeft="1.0px"
android:insetRight="1.0px" >
<selector>
<item android:state_pressed="true">
<shape>
<gradient
android:angle="270.0"
android:endColor="@color/base_end_color_pressed"
android:startColor="@color/base_start_color_pressed" />
<corners
android:bottomLeftRadius="0.0dip"
android:bottomRightRadius="0.0dip"
android:radius="2.0dip"
android:topLeftRadius="10.0dip"
android:topRightRadius="10.0dip" />
<stroke
android:width="1dp"
android:color="#eededede" />
</shape>
</item>
<item>
<shape>
<gradient
android:angle="270.0"
android:endColor="@color/base_end_color_default"
android:startColor="@color/base_start_color_default" />
<corners
android:bottomLeftRadius="0.0dip"
android:bottomRightRadius="0.0dip"
android:radius="2.0dip"
android:topLeftRadius="11.0dip"
android:topRightRadius="11.0dip" />
<stroke
android:width="1dp"
android:color="#eededede" />
</shape>
</item>
</selector>
</inset>
這裡有個容易忽略的小細節:兩個 item 的 topLeftRadius 一個是 10dip、一個是 11dip。這不是筆誤,而是為了補償 pressed 狀態下 stroke 視覺上的差異,避免按下瞬間圓角看起來「跳一下」。這類像素級的細節,通常要實機反覆比對才調得出來。
這是留言區最熱的問題。Android 4.0 之後,硬體加速預設開啟,而虛線在硬體加速下會被繪製成實線。解法是對該 View 關閉硬體加速。
程式碼中加上:
line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
或者在 XML 中加上:
android:layerType="software"
原因說明:早期的 PaintDrawable 在硬體加速管線下不支援 dash path effect,強行走硬體繪製就會退化成實線。指定 software layer 會讓這個 View 改用軟體繪製,虛線就回來了。代價是該 View 會佔用額外記憶體且效能略差,所以只對需要的 View 局部關閉即可,不要全域關掉硬體加速。
感謝留言區幾位朋友的參與與指正。
android:background="@drawable/xxx" 裡引用,不必額外寫 style。<stroke> 是做不出來的,改用 <layer-list> 疊一層,或直接用 MaterialDivider。MaterialCardView 的 cardCornerRadius、cardElevation 屬性取代,程式碼更少也更好維護。sweep(扇形)或 radial(放射),XML 的 <gradient> 也支援 android:type 與 android:gradientRadius。原始碼可於原文頁面下載。
Android Studio 基本使用全面指南
本文總結了一段時間以來對 Android Studio 的使用心得,也希望能給剛接觸的朋友一些參考。此前一直使用 Eclipse,開發體驗也不錯,但看到越來越多人轉向 A
安卓學習之路:用JAVA多線程實現斷點續傳下載
本文示範如何用 JAVA 多線程實現檔案下載(代碼尚未優化,先實現功能)。核心思路是:先取得檔案總長度並建立同大小的佔位檔案,再將檔案切分成若干區塊,每個線程透過 HTT
Android Content Provider解析:跨應用共享數據的標準方式
什麼是Content Provider? Content Provider是Android中用於在不同應用之間共享數據的元件。它負責將數據集中管理,並讓其他應用透過標準介
Android Studio 單元測試設定與執行指南
撰寫測試類別建立測試資料夾與測試套件。新增 ExampleTest,並繼承 InstrumentationTestCase。建立以 test 開頭的方法,例如 testJ