編輯:關於 Android 程式設計
初學 Android 時,最常有的疑問就是「這個資料夾是幹什麼的」、「我的圖片該放哪裡」。以下把標準專案結構逐一說明,並釐清 res 底下幾個容易混淆的子目錄。
1. src:存放所有的 *.java 原始碼。
2. gen:ADT 插件自動產生的程式碼存放路徑,其中的 R.java 保存所有資源的 ID 對應。這個目錄不該手動修改,也不該納入版本控制。
3. assets:存放專案中較大的資源檔,例如圖片、音樂、字型。與 res 最大的差異是:assets 中的檔案不會被產生資源 ID,必須用 AssetManager 以檔案名稱字串存取。
4. res:存放專案所有會被編入資源表的檔案,例如圖片(.png、.jpg)與文字。
drawable-hdpi / drawable-ldpi / drawable-mdpi:分別保存高、低、中等解析度的圖片資源,皆可用 Resources.getDrawable(id) 取得。系統會依裝置螢幕密度自動挑選合適的一份,這也是為什麼同一張圖要準備多個版本——直接只放一份大圖會造成低階機型記憶體浪費。
layout:存放所有版面配置檔,用於排列不同的顯示元件。
values:存放資源值檔案,用於讀取文字等資源。這個目錄下有幾個約定俗成的檔名:
attrs.xml — 定義自訂屬性值。
arrays.xml — 定義陣列資料:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<string-array name="MyArray">
<item>第一</item>
<item>第二</item>
<item>第三</item>
<item>第四</item>
</string-array>
</resources>
在程式碼中讀取:
Resources r = this.getResources(); String[] ary = r.getStringArray(R.array.MyArray);
colors.xml — 定義顏色數值。
dimens.xml — 定義尺寸,可用 Resources.getDimension() 取得。
strings.xml — 定義字串,可用 Resources.getString() 或 getText() 取得。
styles.xml — 定義顯示樣式:
<resources xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 基礎主題,在較新裝置上會被 res/values-vXX/styles.xml 中的版本取代 -->
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- 應用程式主題 -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowNoTitle">true</item>
</style>
<!-- 全域的 Activity 切換動畫 -->
<style name="MyTheme" parent="AppTheme">
<item name="android:windowAnimationStyle">@style/ActivityAnimation</item>
</style>
<style name="ActivityAnimation">
<!-- 開啟 activity,新頁面的進入動畫 -->
<item name="android:activityOpenEnterAnimation">
@anim/push_right_in</item>
<!-- 開啟 activity,原頁面的離開動畫 -->
<item name="android:activityOpenExitAnimation">
@anim/push_left_out</item>
<!-- 關閉 activity,原頁面的進入動畫 -->
<item name="android:activityCloseEnterAnimation">
@anim/push_left_out</item>
<!-- 關閉 activity,當前頁面的離開動畫 -->
<item name="android:activityCloseExitAnimation">
@anim/push_right_out</item>
</style>
</resources>
raw:存放直接複製到裝置中的原生檔案,如音樂、影片。這些檔案無需編譯,會原封不動被打進 APK。使用時呼叫 Resources.openRawResource(),參數是資源 ID(即 R.raw.somfilename)。與 assets 的差異是 raw 中的檔案會被產生 ID。
xml:使用者自訂的 XML 檔,編譯進應用程式後可用 Resources.getXML() 取得。
anim:定義動畫物件,存放補間動畫(tweened animation)或逐幀動畫(frame by frame animation)的 XML。
animator:存放定義 property animation(Android 3.0 引入的動畫框架)的 XML。與 anim 分開放置是好習慣,兩者的 API 與語意完全不同。
color:存放 Color State List 資源的 XML。
drawable:存放圖片狀態列表資源的 XML,也放一般的圖片檔。
menu:存放應用程式選單資源的 XML。建立方式:new Folder 名為 menu,再 new Other → Android → XML,選擇 menu 類型。選單檔必須放在 res/menu 下,且必須以 <menu> 作為根節點,另外兩個可用標籤是 <item>(選單項)與 <group>(分組)。
這兩個目錄最容易搞混,實際上有明確的規則。
res/color 資料夾放的 color_selector.xml 主要用於文字顏色的狀態切換,例如 TextView 的 textColor 屬性:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:color="@color/not_pressed" />
<item android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:color="@color/not_pressed" />
<item android:state_selected="true"
android:color="@color/pressed" />
<item android:state_focused="true"
android:color="@color/pressed" />
<item android:state_pressed="true"
android:color="@color/pressed" />
</selector>
這裡使用的是 android:color 屬性,引用在 values/color.xml 中定義的色值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="not_pressed">#000000</color>
<color name="pressed">#ffffff</color>
</resources>
res/drawable 資料夾中的 drawable_selector.xml 主要用於背景圖的狀態切換,例如 Button 的 android:background:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="@drawable/btn_normal_bg" />
<item android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/btn_normal_bg" />
<item android:state_selected="true"
android:drawable="@drawable/btn_pressed_bg" />
<item android:state_focused="true"
android:drawable="@drawable/btn_pressed_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_pressed_bg" />
</selector>
這裡使用的是 android:drawable,引用的圖片資源放在 drawable 各資料夾下。
對照上面兩段就會發現:color_selector.xml 中沒有 android:drawable,drawable_selector.xml 中沒有 android:color。Android 是依資料夾位置來判斷 selector 中的屬性型別——color 資料夾下的 selector 用 color 屬性,drawable 資料夾下的用 drawable 屬性。
因此屬性引用不能亂接:若 textColor 引用了 drawable_selector.xml,或 background 引用了 color_selector.xml,都會在編譯或執行時報錯。這點要特別注意。
假設現在的需求是「Button 點擊時變綠色、不點擊時是紅色」,沒有設計師提供的切圖可用。這時該怎麼辦?
因為要改的是 background,所以仍然使用 drawable_selector.xml,只需把其中的 android:drawable="@drawable/btn_normal_bg" 改成 android:drawable="@color/not_pressed" 即可。
這裡常見一個誤解:以為 android:drawable 只能引用 drawable 資源。實測證明不然——它同樣可以引用 color 資源。原理在於 selector 的 item 本來就支援 color 與 drawable 兩種類型,而資料夾歸屬決定的只是「這個 selector 檔案被拿去做哪種用途」。
不過要提醒的是,這種「用 color 充當 background」的做法在 Android 5.0 之後有更妥善的方案:android:backgroundTint 或 Material Design 的 ColorStateList 綁定,能避免把純色填進需要 Nine-Patch 或形狀的場合。
實務上建議對 selector 檔採「用途 + _selector」的命名,例如 text_color_selector.xml、btn_bg_selector.xml,一眼就能看出該在哪裡引用。另外,values 資料夾的檔名雖然技術上不強制,但 strings.xml、colors.xml、dimens.xml、styles.xml 這組命名是 Android Studio 與多數工具預設會搜尋的,偏離慣例可能造成工具支援度下降。
音樂播放器App框架設計與實踐總結
音樂播放器 App 通常由界面層與服務層共同構成。界面負責展示音樂列表、歌手、專輯、播放詳情與搜索結果;服務則承擔音樂掃描、播放控制、後台續航與通知欄交互等核心能力。 界
底部導航菜單實戰:FragmentTabHost + ViewPager + Fragment
前言 在 Android 開發裡,底部菜單欄幾乎是 App 的標配。實現方式其實有不少選擇:早期的 TabWidget、隱藏 TabWidget 後用 RadioGrou
Android 下拉刷新框架實現代碼詳解
前段時間項目裡要用下拉刷新,網上找過一些 demo,但質量參差不齊:有的用戶體驗差,有的接口設計不合理。最後實在看不下去,索性自己寫了一個通用框架,效果和擴展性都還不錯,
Java/Android 設計模式筆記(11):原型模式 Prototype
本文要談的是最後一個建立型模式——原型模式(Prototype Pattern)。這個模式先準備一個樣板物件,再由樣板複製出內部屬性一致的執行個體,這個複製的動作在 C+