Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用線上工具產生 ActionBar Style,再手動微調樣式

編輯:關於 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 指到產生的主題即可。

工具產生的原始 style

<!-- 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>

實際跑起來的樣子

橫向/

此時標題欄背景常常還差一點意思,這就需要手動改配置了。

手動微調後的 style

下面這份是我調整過的版本,加上了註解,也把一些常用但預設關閉的項目以註解形式留在檔案裡,方便你自行試著打開:

<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 一類的項目自行設定。

補充:values-v11 與 values-v14

為什麼產生的資源裡會出現帶版本後綴的資料夾?因為 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。

理解了這個資源限定符機制,之後要針對不同版本微調外觀就輕鬆多了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved