編輯:Android 開發教學
最終效果如下:

具體來說實現了兩件事:CheckBox 自定義選中與未選中樣式,以及菜單項根據所在位置設置不同的背景圖(首項圓角、末項圓角、中間項直角)。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFE1E0"
android:orientation="vertical">
<LinearLayout
style="@style/SettingItemTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/MySettingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟通知" />
<CheckBox
style="@style/MyCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
style="@style/SettingItemMiddle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/MySettingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟鈴聲" />
<CheckBox
style="@style/MyCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
style="@style/SettingItemBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="@style/MySettingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟震動" />
<CheckBox
style="@style/MyCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
注意這一句:style="@style/MySettingText"。這裡用 style.xml 來控制樣式。
Android 實際開發中,同一個部件的屬性組合會反覆出現。如果每個控件都手工輸入一遍這些屬性,不僅低效,後續修改時也容易漏改。把重複的屬性集合定義成一個樣式,用到時引用即可。
style="@style/MySettingText" 控制文字的屬性,代碼如下:
<style name="MySettingText">
<item name="android:layout_margin">10dp</item>
<item name="android:layout_weight">6</item>
</style>
這裡只用了兩個屬性,開發中可按實際需要自行增減。以後每個設置項的文字不必再重複添加,引用一下就行。
這裡的 layout_weight=6 配合 CheckBox 的 layout_weight=1,實現了「文字佔六份、開關佔一份」的比例佈局。這種用權重而非固定 dp 的做法,能在不同屏幕密度下保持一致的比例關係,比寫死寬度健壯得多。
先看 CheckBox 引用的樣式:
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/check</item>
<item name="android:layout_margin">10dp</item>
<item name="android:layout_weight">1</item>
</style>
注意 parent 屬性:它繼承了系統的 CheckBox 默認樣式,我們只覆寫需要改的三項。這是樣式繼承的標準用法,比從零定義更安全。
關鍵是這一句:
<item name="android:button">@drawable/check</item>
它把 CheckBox 的按鈕部分替換成自定義的 check 資源,而這個資源使用了 selector。
在 res/drawable 下建立名為 check.xml 的文件:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/on" />
<item android:state_checked="false"
android:drawable="@drawable/off" />
</selector>
其中 @drawable/on 是選中時顯示的圖像,@drawable/off 反之。
state_checked 只是 selector 的一種狀態判據。實際開發中常用的還包括:
android:state_pressed:按壓狀態,做按鈕點擊回饋的首選;android:state_enabled:控件是否可用,做置灰效果;android:state_focused:焦點狀態;android:state_selected:選中狀態,常用於列表項高亮。selector 的 item 是有順序的:系統從上往下匹配,第一個滿足的即生效。所以更通用的條件要放在後面,更具體的放在前面,否則會被提前匹配掉。
另外,selector 裡除了 android:drawable 指定圖片,還可以用 android:color 做顏色變化,或者嵌套 shape 定義圓角、漸變、描邊等形狀,足以應付絕大多數界面裝飾需求。
菜單第一項上方圓角、中間項四角皆方、最後一項下方圓角,這個效果是通過給三個 LinearLayout 分別設置不同的背景圖片實現的。
除了切圖,也可以用 shape drawable 實現同樣效果:定義三個不同的 shape 文件,只在上兩個角或下兩個角設置 <corners android:topLeftRadius="10dp" ... />,其餘角設為 0。這種方式無需切圖、支持任意尺寸拉伸,是更推薦的做法。
不過要注意,如果列表項數量會動態變化,硬編碼「首項、中間項、末項」三種樣式就不合適了,此時應該在 Adapter 中根據 position 動態切換背景資源。
Android 選單(三):先寫好兩個共用輔助方法
選單用來顯示額外選項,這些選項不必佔用主介面的空間。Android 框架中主要有兩種選單: 選項選單 —— 顯示與當前 Activity 相關的資訊,按 MENU 鍵觸發
Genymotion 模擬器安裝與使用教程
好一陣子沒碰 Android,以至於 Genymotion 推出這麼久我居然還沒試過。當年 Android Studio 一發布,我當天就上手了;這回會注意到 Genym
Android 虛擬機全解讀:Dalvik、JVM 與 ART 的異同
Android 5.0 之前使用 Dalvik 虛擬機,它在 JVM 基礎上做了針對移動設備的優化;5.0 之後則換成了 ART。本文從概念出發,把 Dalvik、JVM
Android 手機間無線互傳的技術選型與實作
先前研究了一下 Android 該怎麼做到無線互傳,寫了個小 demo,最近整理出來與大家分享。 技術選型 1. 藍牙:歷史悠久、簡單易用,但速度實在太慢,是致命傷。 2