編輯:關於 Android 程式設計
ActionBar 這個頂部導覽列從 Android 3.0 起就是標準配備,相信大家都已經很熟悉。本文要介紹的不是 API 用法,而是一個能大幅省時的資源:**Android Action Bar Style Generator** 這個線上樣式產生器,以及產生之後如何手動微調。
工具網址:https://jgilfelt.github.com/android-actionbarstylegenerator/
它會一次幫你產出所需的 nine-patch 圖片、drawable 資源以及完整的 style.xml,省去自己切圖的工夫。



看完這幾張截圖,大概就知道它怎麼用了:填幾個顏色選項,下載 zip 解壓後把資源丟進專案對應目錄即可。


把各資料夾的檔案分別複製到專案的同名目錄,然後在 AndroidManifest 中把 android:theme 指到產生的主題即可。
<!-- File created by the Android Action Bar Style Generator
Licensed under the Apache License, Version 2.0 -->
<resources>
<style name="Theme.Test" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarItemBackground">@drawable/selectable_background_test</item>
<item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Test</item>
<item name="android:actionDropDownStyle">@style/DropDownNav.Test</item>
<item name="android:actionBarStyle">@style/ActionBar.Transparent.Test</item>
<item name="android:actionModeBackground">@drawable/cab_background_top_test</item>
<item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_test</item>
<item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Test</item>
</style>
<style name="ActionBar.Solid.Test" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:background">@drawable/ab_solid_test</item>
<item name="android:backgroundStacked">@drawable/ab_stacked_solid_test</item>
<item name="android:backgroundSplit">@drawable/ab_bottom_solid_test</item>
<item name="android:progressBarStyle">@style/ProgressBar.Test</item>
</style>
<style name="ActionBar.Transparent.Test" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@drawable/ab_transparent_test</item>
<item name="android:progressBarStyle">@style/ProgressBar.Test</item>
</style>
<style name="PopupMenu.Test" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>
</style>
<style name="DropDownListView.Test" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background_test</item>
</style>
<style name="ActionBarTabStyle.Test" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_test</item>
</style>
<style name="DropDownNav.Test" parent="@android:style/Widget.Holo.Light.Spinner">
<item name="android:background">@drawable/spinner_background_ab_test</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>
<item name="android:dropDownSelector">@drawable/selectable_background_test</item>
</style>
<style name="ProgressBar.Test" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal_test</item>
</style>
<style name="ActionButton.CloseMode.Test" parent="@android:style/Widget.Holo.Light.ActionButton.CloseMode">
<item name="android:background">@drawable/btn_cab_done_test</item>
</style>
<!-- 僅在 Light.DarkActionBar 主題下被引用 -->
<style name="Theme.Test.Widget" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
</style>
</resources>


橫向/

此時標題欄背景常常還差一點意思,這就需要手動改配置了。
下面這份是我調整過的版本,加上了註解,也把一些常用但預設關閉的項目以註解形式留在檔案裡,方便你自行試著打開:
<resources>
<style name="Theme.Test" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarItemBackground">@drawable/selectable_background_test</item>
<item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
<item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Test</item>
<item name="android:actionDropDownStyle">@style/DropDownNav.Test</item>
<item name="android:actionBarStyle">@style/ActionBar.Transparent.Test</item>
<item name="android:actionModeBackground">@drawable/cab_background_top_test</item>
<item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_test</item>
<item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Test</item>
<!-- 更換返回箭頭圖示 -->
<!--<item name="android:homeAsUpIndicator">@drawable/ic_home_up_indicator</item>-->
<!-- 所有 Activity 的背景 -->
<!--<item name="android:windowBackground">@drawable/activity_bk</item>-->
<!-- 對話框樣式 -->
<!--<item name="android:alertDialogStyle">@style/AlertDialog_Originaltheme</item>-->
</style>
<style name="ActionBar.Solid.Test" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:background">@drawable/ab_solid_test</item>
<item name="android:backgroundStacked">@drawable/ab_stacked_solid_test</item>
<item name="android:backgroundSplit">@drawable/ab_bottom_solid_test</item>
<item name="android:progressBarStyle">@style/ProgressBar.Test</item>
</style>
<!-- 這裡把背景換成不透明版本 -->
<style name="ActionBar.Transparent.Test" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@drawable/ab_solid_test</item>
<item name="android:progressBarStyle">@style/ProgressBar.Test</item>
<!-- 標題文字樣式 -->
<!--<item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle.Originaltheme</item>-->
</style>
<style name="PopupMenu.Test" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>
</style>
<style name="DropDownListView.Test" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background_test</item>
</style>
<style name="ActionBarTabStyle.Test" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_test</item>
</style>
<style name="DropDownNav.Test" parent="@android:style/Widget.Holo.Light.Spinner">
<item name="android:background">@drawable/spinner_background_ab_test</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_test</item>
<item name="android:dropDownSelector">@drawable/selectable_background_test</item>
</style>
<style name="ProgressBar.Test" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/progress_horizontal_test</item>
</style>
<style name="ActionButton.CloseMode.Test" parent="@android:style/Widget.Holo.Light.ActionButton.CloseMode">
<item name="android:background">@drawable/btn_cab_done_test</item>
</style>
<style name="Theme.Test.Widget" parent="@android:style/Theme.Holo">
<item name="android:popupMenuStyle">@style/PopupMenu.Test</item>
<item name="android:dropDownListViewStyle">@style/DropDownListView.Test</item>
</style>
</resources>

改了 android:background 之後,標題欄底色就對了。標題字型顏色、返回箭頭圖示這些也都能透過 titleTextStyle、homeAsUpIndicator 一類的項目自行設定。
為什麼產生的資源裡會出現帶版本後綴的資料夾?因為 ActionBar 相關樣式在不同 API Level 上的可用性不同:
values-v11:在 API 11 以上(Android 3.0+)的裝置上,使用該目錄下的 styles.xml 取代 res/values/styles.xml;values-v14:在 API 14 以上(Android 4.0+)的裝置上,同樣取代預設的 styles.xml。理解了這個資源限定符機制,之後要針對不同版本微調外觀就輕鬆多了。
Android 圖像處理:自定義 View 實作鏡面效果
鏡面效果常見於圖片展示、卡片設計與啟動頁視覺。它通常會在圖片下方產生一個上下翻轉的倒影,並透過漸層遮罩模擬光線衰減。在 Android 中,除了直接處理 Bitmap,也
仿 RadioGroup 打造可複用的單選卡片容器(以底部導航/Tab 為例)
需求很直觀:一排四張樣式相同、內容不同的卡片,同一時間只能有一張處於選中狀態,選中後回傳對應的金額。圖上只是示意效果,實際資料要從伺服器拉回來再灌進控制項。 先盤點幾種
動手寫一個 Android 文件管理器:項目資源層梳理
文件管理器是幫助用戶處理本地與網絡文件的工具,基本能力包括創建、打開、查看、編輯、移動、刪除,很多還擴展了網絡訪問、應用管理、解壓縮與搜索。動手實現一個,能把 Activ
Android 系統開發(一)——GCC 編譯器的編譯與安裝
GCC 編譯器介紹 GCC(GNU C Compiler)是 GNU 專案中符合 ANSI C 標準的編譯系統,可編譯 C、C++、Objective C 等語言,同時也