設定ActionBar的樣式,是我們必須掌握的技能,在之前我們可能都需要一行一行的寫代碼,然后在模擬器上測試效果,但是現在我們有個一個很棒的工具來設定樣式。設定ActionBar樣式的工作從未如此簡單過!
http://romannurik.github.io/AndroidAssetStudio/index.html
進入后我們就可以直接在可視化的界面中進行修改了,設定好后直接下載壓縮包。復制到res目錄下即可。最后要記得把Application或者Activity的主題修改為你做的主題名哦~
順便補充一個設置ActionBar背景的方法:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.id.icon));
下面是我這個主題的style:styles_mycompattheme.xml(這里的文件是V14的,詳細的請下載源碼)
<?xml version="1.0" encoding="utf-8"?> <!-- File created by the Android Action Bar Style Generator Copyright (C) 2011 The Android Open Source Project Copyright (C) 2012 readyState Software Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <resources> <style name="Theme.Mycompattheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <item name="android:actionBarItemBackground">@drawable/selectable_background_mycompattheme</item> <item name="android:popupMenuStyle">@style/PopupMenu.Mycompattheme</item> <item name="android:dropDownListViewStyle">@style/DropDownListView.Mycompattheme</item> <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Mycompattheme</item> <item name="android:actionDropDownStyle">@style/DropDownNav.Mycompattheme</item> <item name="android:actionBarStyle">@style/ActionBar.Solid.Mycompattheme</item> <item name="android:actionModeBackground">@drawable/cab_background_top_mycompattheme</item> <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_mycompattheme</item> <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Mycompattheme</item> <!-- Light.DarkActionBar specific --> <item name="android:actionBarWidgetTheme">@style/Theme.Mycompattheme.Widget</item> </style> <style name="ActionBar.Solid.Mycompattheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/ab_background_textured_mycompattheme</item> <item name="android:backgroundStacked">@drawable/ab_stacked_solid_mycompattheme</item> <item name="android:backgroundSplit">@drawable/ab_background_textured_mycompattheme</item> <item name="android:progressBarStyle">@style/ProgressBar.Mycompattheme</item> </style> <style name="ActionBar.Transparent.Mycompattheme" parent="@style/Widget.AppCompat.ActionBar"> <item name="android:background">@drawable/ab_transparent_mycompattheme</item> <item name="android:progressBarStyle">@style/ProgressBar.Mycompattheme</item> </style> <!-- this style is only referenced in a Light.DarkActionBar based theme --> <style name="Theme.Mycompattheme.Widget" parent="@style/Theme.AppCompat"> <item name="android:popupMenuStyle">@style/PopupMenu.Mycompattheme</item> <item name="android:dropDownListViewStyle">@style/DropDownListView.Mycompattheme</item> </style> </resources>
當然,如果你覺得你想修改Menu和Tab上面的字體,設置個顏色什么的。那么下面的設置一定可以滿足你的需要。
更詳細的定義可以參考:http://blog.csdn.net/gebitan505/article/details/12157027
這里定義了Tab中字體的大小和顏色,定義了Menu的字體的大小和顏色,定義了返回按鈕和分割欄的圖片。
部分代碼如下,這個是在之前的代碼中修改的。
<style name="Theme.Mycompattheme" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <!-- 定義ActionBar左邊小箭頭的圖片 --> <item name="android:homeAsUpIndicator">@drawable/ic_search</item> <!-- 設置ActionBar的高度 --> <item name="android:actionBarSize">60dip</item> <!-- 設置Menu的顏色 --> <item name="android:actionMenuTextColor">#ff0000</item> <!-- 設置Menu的字體樣式 --> <item name="android:actionMenuTextAppearance">@style/MyMenuTextStyle</item> <!-- 設置ActionBar Tab字體的樣式 --> <item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item> <!-- 定義Tab之間的分割線的圖片 --> <item name="android:actionBarDivider">@drawable/ic_search</item>
省略了由工具生成的代碼……
</style> <!-- Menu的樣式 --> <style name="MyMenuTextStyle"> <item name="android:textSize">30sp</item> </style> <!-- Tab字體的顏色、字體等樣式 --> <style name="MyTabTextStyle" parent="@style/Widget.AppCompat.Light.ActionBar.TabText"> <item name="android:textColor">#0000ff</item> <item name="android:textSize">18sp</item> </style>
至於定義actionbar上面的進度條樣式的可以看這篇文章:http://www.myexception.cn/mobile/1356045.html
更加詳細全面的ActionBar的樣式的修改可以看這里:http://blog.csdn.net/guolin_blog/article/details/25466665 (推薦)
源碼下載:http://download.csdn.net/detail/shark0017/7690915