編輯:關於 Android 程式設計
很久沒更新部落格,很多東西用完就忘,實在該記下來。除了自己複習,也推薦讀者養成寫筆記的習慣——開發 App 時真正用到的知識遠比一開始想像的多,甚至還會中途被派去用別的語言,不記錄就是學一個忘一個。
今天談兩件事:一是 drawable 在實務上的小技巧,二是 Android 2.x 上的一個圓角 BUG。
看到這樣的按鈕:

直覺做法是請美工切一張圓角背景圖,但多尺寸螢幕適配時還得再用 9-patch 處理一次,相當麻煩。其實只要是純色或簡單漸層,寫一份 drawable XML 就足夠了。
/drawable/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#ff0bd38a" />
</shape>使用方式與一般圖片完全相同:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_bg"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="25sp" />android:shape 決定基本形狀,除了 rectangle(矩形)還有 oval(橢圓)、line(線段)、ring(圓環),後三者實務上使用頻率不高。
android:radius 為圓角半徑,數值越大越圓。四個角也可以各自指定:
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp" />solid 是填色,android:color 指定顏色。stroke 則是描邊:
<stroke
android:width="1dp"
android:color="#ffaaaaaa" />描邊還能做成虛線:android:dashWidth 是每一段實線的長度,android:dashGap 是段與段之間的間距。
需要邊框的場合寫成這樣即可:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#ffaaaaaa" />
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />
</shape>android:startColor 與 android:endColor 分別是起始與結束色;android:angle 是漸層方向,且必須是 45 的整數倍。預設模式 android:type="linear" 為線性漸層,若要徑向漸層則設 android:type="radial" 並指定半徑 android:gradientRadius。
角度的意義可以這樣理解:把控制項內部想像成一塊色板,angle=0 由左至右、90 由上至下、180 由右至左,以此類推,數值就是色板繞中心的旋轉角度。
實務上漸層不太好調,多色變化又難拿捏,除非確定適用,否則直接請美工出圖反而更快。
定義內容與邊界之間的距離,寫法單純,不多贅述。
用 layer-list 搭配 clip,就能做出圓角進度條:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="#ffffffff" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#ff00ff00" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#ff00ff00" />
</shape>
</clip>
</item>
</layer-list>使用時指定 android:progressDrawable 即可:
<ProgressBar
android:layout_width="match_parent"
android:layout_height="2dp"
android:progress="50"
android:progressDrawable="@drawable/progress_horizontal" />
前述「四個角各自設定」的寫法,在 Android 2.x 上有個坑:bottomLeftRadius 與 bottomRightRadius 的實際效果是對調的。
照直覺寫:
<corners
android:bottomLeftRadius="18dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="18dp"
android:topRightRadius="0dp" />
2.x 上的結果左右顛倒。若為了遷就 2.x 而交換寫法,到了 3.x 以上又會變成錯誤:

在 res/ 下新建 drawable-v12 目錄,把正確的寫法放進去;drawable/ 則保留遷就 2.x 的版本。因為 3.x 以上會優先從帶版本限定符的目錄取資源,兩邊就能各自拿到正確結果。
修改後的效果:

這個「用資源限定符做版本分流」的思路可以類推:以后再遇到类似在不同系統版本上行為不一致的問題,都能用同樣手法隔開,而不必在程式碼裡到處寫版本判斷。
Android四大組件之Service進階
一、前言 上一節我們學習了 Service 的生命周期,以及啟動 Service 的兩種方法。本節將繼續深入了解 Service 中的 IntentService,並通過
Android 點擊事件分發機制實驗
事件分發的三個核心方法 Android 點擊事件分發,圍繞 MotionEvent 的傳遞。核心方法包括 dispatchTouchEvent、onInterceptTo
AndroidAnnotations 開發環境配置詳解(Eclipse 版)
過去介紹過 xUtils 這類國內常見的快捷開發框架,但它的註解機制穩定性與可選項目都略嫌不足。AndroidAnnotations 則是國外較早投入註解導向開發的框架,
OSGi 模組間通訊:EventAdmin 發布與訂閱實作
前言 在 OSGi 環境中,Bundle 之間需要低耦合地交換訊息。EventAdmin 提供了一種基於發布與訂閱的通訊方式:發布者把事件送到事件主題,訂閱者註冊有興趣的