Styling the Action Bar


  If you want to implement a visual design that represents your app's brand, the action bar allows you to customize each detail of its appearance, including the action bar color, text colors, button styles, and more. To do so, you need to use Android's style and theme framework to restyle the action bar using special style properties.

Caution: For all background drawables you provide, be sure to use Nine-Patch drawables to allow stretching. The nine-patch image should be smaller than 40dp tall and 30dp wide.

注意:背景图片最好用.9图片
以下是自定义样式时重要的样式属性,注意xxxStyle 的值是另一个样式元素或文件.
General appearance 通用样式属性
actionBarStyle Specifies a style resource that defines various style properties for the action bar.

The default for this style for this is Widget.AppCompat.ActionBar, which is what you should use as the parent style.

Supported styles include:

background Defines a drawable resource for the action bar background.
backgroundStacked Defines a drawable resource for the stacked action bar (the tabs).
backgroundSplit Defines a drawable resource for the split action bar.
actionButtonStyle

Defines a style resource for action buttons.

The default for this style for this is Widget.AppCompat.ActionButton, which is what you

should use as the parent style.

actionOverflowButtonStyle Defines a style resource for overflow action items.

The default for this style for this is Widget.AppCompat.ActionButton.Overflow,

which is what you should use as the parent style.

displayOptions

Defines one or more action bar display options, such as whether to use the app logo, show the activity

title, or enable the Up action.See displayOptions for all possible values.

divider Defines a drawable resource for the divider between action items.
titleTextStyle

Defines a style resource for the action bar title.

The default for this style for this is TextAppearance.AppCompat.Widget.ActionBar.Title,

which is what you should use as the parent style.

windowActionBarOverlay

  Declares whether the action bar should overlay the activity layout rather than offset the activity's layout position (for example,

the Gallery app uses overlay mode). This is false by default.

  Normally, the action bar requires its own space on the screen and your activity layout fills in what's left over. When the action bar is

in overlay mode, your activity layout uses all the available space and the system draws the action bar on top. Overlay mode can be

useful if you want your content to keep a fixed size and position when he action bar is hidden and shown.You might also like to use

it purely as a visual effect, because you can use a semi-transparent background for the action bar so the user can still see some of

your activity layout behind the action bar.

  Note: The Holo theme families draw the action bar with a semi-transparent background by default. However, you can modify it

with your own styles and the DeviceDefault theme on different devices might use an opaque background by default.

  When overlay mode is enabled, your activity layout has no awareness of the action bar lying on top of it. So, you must be careful

not to place any important information or UI components in the area overlaid by the action bar.If appropriate, you can refer to the

platform's value for actionBarSize to determine the height of the action bar,

by referencing it in your XML layout. For example:

<SomeView
    ...
    android:layout_marginTop="?android:attr/actionBarSize" />

  You can also retrieve the action bar height at runtime with getHeight(). This reflects the height of the action bar at the time it's

called, which might not include the stacked action bar (due to navigation tabs) if called during early activity lifecycle methods. To see

how you  can determine the total height at runtime, including the stacked action bar, see the TitlesFragment class in the

Honeycomb Gallery  sample app.

   Action items 操作项样式属性
actionButtonStyle Defines a style resource for the action item buttons.

The default for this style for this is Widget.AppCompat.ActionButton, which is what you should use as the parent style.

actionBarItemBackground

Defines a drawable resource for each action item's background. This should be a state-list drawable to indicate different selected

states.

itemBackground

Defines a drawable resource for each action overflow item's background. This should be a state-list drawable to indicate different

selected states.

actionBarDivider Defines a drawable resource for the divider between action items.
actionMenuTextColor Defines a color for text that appears in an action item.
actionMenuTextAppearance Defines a style resource for text that appears in an action item.
actionBarWidgetTheme Defines a theme resource for widgets that are inflated into the action bar as action views.
Navigation tabs 顶部Tab样式属性

actionBarTabStyle

Defines a style resource for tabs in the action bar.

The default for this style for this is Widget.AppCompat.ActionBar.TabView, which is what you should use as the parent style.

actionBarTabBarStyle Defines a style resource for the thin bar that appears below the navigation tabs.

The default for this style for this is Widget.AppCompat.ActionBar.TabBar, which is what you should use as the parent style.

actionBarTabTextStyle  Defines a style resource for text in the navigation tabs.

The default for this style for this is Widget.AppCompat.ActionBar.TabText, which is what you should use as the parent style.

  Drop-down lists 下拉列表样式属性
 actionDropDownStyle Defines a style for the drop-down navigation (such as the background and text styles).

The default for this style for this is Widget.AppCompat.Spinner.DropDown.ActionBar, which is what you should use as the

parent style.

Example theme

  Here's an example that defines a custom theme for an activity, CustomActivityTheme, that includes several styles to customize the action bar.

Notice that there are two versions for each action bar style property. The first one includes the android: prefix on the property name to support API levels 11 and higher that include these properties in the framework. The second version does not include the android: prefix and is for older versions of the platform, on which the system uses the style property from the support library. The effect for each is the same.

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <!-- the theme applied to the application or activity -->
     <style name="CustomActionBarTheme"
            parent="@style/Theme.AppCompat.Light">
         <item name="android:actionBarStyle">@style/MyActionBar</item>
         <item name="android:actionBarTabTextStyle">@style/TabTextStyle</item>
         <item name="android:actionMenuTextColor">@color/actionbar_text</item>

         <!-- Support library compatibility -->
         <item name="actionBarStyle">@style/MyActionBar</item>
         <item name="actionBarTabTextStyle">@style/TabTextStyle</item>
         <item name="actionMenuTextColor">@color/actionbar_text</item>
     </style>

     <!-- general styles for the action bar -->
     <style name="MyActionBar"
            parent="@style/Widget.AppCompat.ActionBar">
         <item name="android:titleTextStyle">@style/TitleTextStyle</item>
         <item name="android:background">@drawable/actionbar_background</item>
         <item name="android:backgroundStacked">@drawable/actionbar_background</item>
         <item name="android:backgroundSplit">@drawable/actionbar_background</item>

         <!-- Support library compatibility -->
         <item name="titleTextStyle">@style/TitleTextStyle</item>
         <item name="background">@drawable/actionbar_background</item>
         <item name="backgroundStacked">@drawable/actionbar_background</item>
         <item name="backgroundSplit">@drawable/actionbar_background</item>
     </style>

     <!-- action bar title text -->
     <style name="TitleTextStyle"
            parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
         <item name="android:textColor">@color/actionbar_text</item>
     </style>

     <!-- action bar tab text -->
     <style name="TabTextStyle"
            parent="@style/Widget.AppCompat.ActionBar.TabText">
         <item name="android:textColor">@color/actionbar_text</item>
     </style>
 </resources>

In your manifest file, you can apply the theme to your entire app:

<application android:theme="@style/CustomActionBarTheme" ... />

Or to individual activities:

<activity android:theme="@style/CustomActionBarTheme" ... />

Caution: Be certain that each theme and style declares a parent theme in the <style> tag, from which it inherits all styles not explicitly declared by your theme. When modifying the action bar, using a parent theme is important so that you can simply override the action bar styles you want to change without re-implementing the styles you want to leave alone (such as text size or padding in action items).

For more information about using style and theme resources in your application, read Styles and Themes.

练习示例

 <resources>
     <!--
         Base application theme for API 14+. This theme completely replaces
         AppBaseTheme from BOTH res/values/styles.xml and
         res/values-v11/styles.xml on API 14+ devices.
     -->
     <style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
         <!-- API 14 theme customizations can go here. -->
     </style>

     <!-- 自定义actionBar的样式主题 -->
     <style name="StandardABTheme" parent="AppBaseTheme">

         <!-- 用自定义的ActionBar的standard mode样式 -->
         <item name="android:actionBarStyle">@style/custom_ActionBarStyle</item>

         <!-- 自定义每个可见action item的背景(包括homeButton,action item和溢出button,但不包括溢出菜单里的) -->
         <item name="android:actionBarItemBackground">@drawable/selector_actionbar_item_background</item>        

         <!-- 每个溢出菜单item的样式  -->
         <item name="android:itemBackground">@drawable/selector_actionbar_overflow_item</item>        

         <!-- 定义的ActionBar的溢出按钮(OverflowButton)样式 -->
         <!-- <item name="android:actionOverflowButtonStyle">@style/custom_ActionButton.Overflow</item> -->

         <!-- <item name="android:actionMenuTextColor" >@color/green</item> -->

         <item name="android:actionMenuTextAppearance">@style/custom_ActionMenuTextStyle</item>

            <!-- 自定义ActionBar的溢出菜单框架的样式 -->
            <!-- <item name="android:popupMenuStyle">@style/custom_PopupMenu</item> -->                            

            <!-- 自定义ActionBar的溢出菜单列表的样式 -->
         <!-- <item name="android:dropDownListViewStyle">@style/custom_DropDownListView</item>     -->
            <!-- api21 -->
            <!-- <item name="android:actionOverflowMenuStyle">?attr/popupMenuStyle</item> -->
     </style>
     <style name="custom_ActionMenuTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
         <item name="android:textSize">20sp</item>
         <item name="android:textStyle">bold</item>
         <item name="android:textColor">@color/white</item>
         <item name="android:textAllCaps">false</item>
     </style>
     <!-- 自定义ActionBar的bar的样式 -->
     <style name="custom_ActionBarStyle" parent="@android:style/Widget.ActionBar">
         <!-- Change action bar background color -->
         <item name="android:background">@drawable/selector_actionbar_item_background</item>
         <!-- Change split action bar background color -->
         <item name="android:backgroundSplit">@color/skyblue</item>
         <item name="android:titleTextStyle">@style/custom_TitleTextStyle</item>
     </style>

     <!-- 自定义ActionBar的actionButton样式,选中时,正常时,焦点时 -->
     <style name="custom_ActionButtonStyle" parent="@android:style/Widget.ActionButton">
         <!-- Change action button background color -->
         <item name="android:background">@drawable/selector_actionbar_button</item>
     </style>

     <!-- 自定义ActionBar的OverflowButton样式 -->
     <style name="custom_ActionButton.Overflow" parent="@android:style/Widget.ActionButton.Overflow">
         <!-- Change action bar Overflow button image -->
         <item name="android:src">@drawable/ic_actionbar_overflow_button</item>                         <!-- OverflowButton的图片 -->
         <item name="android:background">@drawable/selector_actionbar_item_background</item>
     </style>

     <!-- 自定义ActionBar的溢出菜单框架的样式 -->
     <style name="custom_PopupMenu" parent="@android:style/Widget.Holo.Light.ListPopupWindow">
         <item name="android:popupBackground">@drawable/popup_menu_frame_background</item>            <!-- 溢出菜单的背景图 -->
     </style>

     <!-- 自定义ActionBar的溢出菜单列表的样式 -->
     <style name="custom_DropDownListView" parent="@android:style/Widget.Holo.Light.ListView.DropDown">
         <item name="android:listSelector">@drawable/popup_menu_list_item_background</item>
     </style>

     <!-- 自定义ActionBar的titleText样式 -->
     <style name="custom_TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
         <!-- Change action bar title color -->
         <item name="android:textColor">@color/white</item>
     </style>

 </resources>

ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)的更多相关文章

  1. ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件

    Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...

  2. ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理

    Adding Action Items The action bar provides users access to the most important action items relating ...

  3. ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)

    This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...

  4. ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标

    Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...

  5. ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器

    Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...

  6. ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示

    Adding the Action Bar As mentioned above, this guide focuses on how to use the ActionBar APIs in the ...

  7. ActionBar官方教程(1)简介及各区域介绍

    Action Bar The action bar is a window feature that identifies the user location, and provides user a ...

  8. ActionBar官方教程(10)ActionBar的下拉列表模式

    Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...

  9. ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间

    Navigating Up with the App Icon Enabling the app icon as an Up button allows the user to navigate yo ...

随机推荐

  1. Ext.Net学习笔记17:Ext.Net GridPanel Selection

    Ext.Net学习笔记17:Ext.Net GridPanel Selection 接下来是Ext.Net的GridPanel的另外一个功能:选择. 我们在GridPanel最开始的用法中已经见识过如 ...

  2. Loadrunner测试json接口

    1. loadrunner + json说明 使用lr测试json接口,向服务端发送json格式请求,接收处理返回响应数据. 主要用到函数: 1)web_custon_request 2)web_re ...

  3. 07_控制线程_join_线程插队

    [join线程简述] join()方法:Thread提供的让一个线程去等待另一个线程完成.当在某个程序执行流中(如main线程)调用其它线程(如t2线程)的join方法(t2.join()),调用线程 ...

  4. VIM 拼写/spell check

    VIM 拼写检查/spell check 一.Hunspell科普 Hunspell 作为一个拼写检查的工具,已经用在了许多开源的以及商业软件中.包括Google Chrome, Libreoffic ...

  5. DLL详解及Denpendcy Walker的使用

    下面的文章被N次转载,为了尊重原作,\(^o^)/~,贴出最早发布这篇文章的地址及作者.   动态链接库 Windows的活动大陆 2006-07-26 09:21  作者:狂ρκ来源:电脑爱好者 在 ...

  6. C++学习之路,漫长而遥远

    一.C/C++语言 如果你的基础很差, 建议不要一开始就学C++语言,从C开始学起,对程序有个初步的认识,循序渐进.C语言的书嘛,先买一本 300 页以内的,把书中的每一个例子都通过键盘敲打进去到 V ...

  7. ajax、json一些整理(3)

    写上面那些都是因为对ajax不熟悉 从w3c抄写JS原生ajax的东西补充一些基础 XMLHttpRequest 是 AJAX 的基础. XMLHttpRequest 对象 所有现代浏览器均支持 XM ...

  8. C#表驱动法+一点反射实现“得到指定位数随机不重复字符串”三种方式的封装

    1.结构 第一个类 public class GetMethods{...}      类中的变量:                                                   ...

  9. 编译报错GLIBCXX_3.4.15 clock_gettime@@GLIBC_2.2

    GLIBCXX_3.4.15 升级gcc,g++编译器 clock_gettime@@GLIBC_2.2 链接库时加-lrt

  10. C#并行和多线程编程_(1)认识Parallel

    Parallel: 英 [ˈpærəlel]    美 [ˈpærəˌlɛl] ,并联的,并行的. 随着多核时代的到来,并行开发越来越展示出它的强大威力!使用并行程序,充分的利用系统资源,提高程序的性 ...