Android官方开发文档Training系列课程中文版:添加ActionBar之自定义ActionBar样式

本文主要是介绍Android官方开发文档Training系列课程中文版:添加ActionBar之自定义ActionBar样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址 : http://android.xsoftlab.net/training/basics/actionbar/styling.html

ActionBar的样式

ActionBar提供了为用户提供了常见的习惯性的用户界面以及按钮功能。但是这并不意味着必须要和其它APP看起来一模一样。如果需要设计更符合产品品牌样式风格的话,ActionBar也可以做到,你可以通过Android的style and theme很容易做到这一点。

Android已经包含了一少部分的内置Activity主题,这些主题包含了”dark”或者”light”的ActionBar风格。你也可以通过继承这些主题来进一步的自定义自己ActionBar。

Note:如果使用的是ActionBar的支持库,那么你必须使用Theme.AppCompat 家族的风格。在这样的情况下,每一个样式属性必须声明两次:一次是在平台样式属性中,一次是在支持库样式属性中。相关情况请看下面的样例。

使用一个Android主题

Android内置了两种最基本的Activity主题,它们区别在于ActionBar的颜色:

  • Theme.Holo 是”dark”主题。

  • Theme.Holo.Light 是”light”主题。

你可以将这些主题应用到整个APP中或者是单个Activity中,通过在清单文件的< application>元素或是< activity>元素内部使用android:theme属性来指定:

<application android:theme="@android:style/Theme.Holo.Light" ... />

你也可以通过使用Theme.Holo.Light.DarkActionBar主题来达到ActionBar是”dark”样式而Activity的其余部分则是”light”样式的效果。

如果是使用了支持库的话,必须使用Theme.AppCompat下的主题:

  • Theme.AppCompat 对应的是”dark”主题.
  • Theme.AppCompat.Light 对应的是”light”主题.
  • Theme.AppCompat.Light.DarkActionBar 对应的是带着黑色ActionBar的亮色主题.

请确保在ActionBar上图标颜色与ActionBar的颜色有适当的对比度。为了辅助达到这一点, Action Bar Icon Pack包含了一些用在ActionBar上的标准的功能按钮。

自定义ActionBar的背景色

为了改变ActionBar的背景色,需要创建一个自定义的主题,然后重写actionBarStyle属性。这个属性指向了其它的背景样式,您可以在其它背景样式中重写background属性来给ActionBar指定一个图像资源。

如果APP使用了navigation tabs或者 split action bar,你也可以分别通过backgroundStacked和backgroundSplit属性指定他们的背景色。

注意: 选择适合的父类主题对于自定义主题来说很重要,当继承主题之后,所有的样式都会被继承下来。如果没有父类主题,除非很明确的声明了每一项样式,否则ActionBar会丢失很多样式属性。

为Android 3.0及以上版本定义

当仅仅支持了Android 3.0及更高的版本,你可以像这样定义ActionBar的背景:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@android:style/Theme.Holo.Light.DarkActionBar"><item name="android:actionBarStyle">@style/MyActionBar</item></style><!-- ActionBar styles --><style name="MyActionBar"parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"><item name="android:background">@drawable/actionbar_background</item></style>
</resources>

然后应用这个主题到整个APP中或者是单个Activity中:

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

为Android 2.1及以上版本定义

当使用了支持库,它的修改方式和上面很相似:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.AppCompat.Light.DarkActionBar"><item name="android:actionBarStyle">@style/MyActionBar</item><!-- Support library compatibility --><item name="actionBarStyle">@style/MyActionBar</item></style><!-- ActionBar styles --><style name="MyActionBar"parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"><item name="android:background">@drawable/actionbar_background</item><!-- Support library compatibility --><item name="background">@drawable/actionbar_background</item></style>
</resources>

然后应用这个主题到整个APP中或者是单个Activity中:

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

自定义字体颜色

为了改变ActionBar上的字体颜色,你需要单独重写text元素的每一个属性:

  • ActionBar标题:创建自定义的样式,然后指定textColor的值,然后将这个样式应用到你自己定义的主题actionBarStyle中的titleTextStyle属性里。

注意:如果要使用应用到titleTextStyle中的自定义样式,那么应该使这个样式继承TextAppearance.Holo.Widget.ActionBar.Title。

  • ActionBar的标签:重写主题中的actionBarTabTextStyle。
  • ActionBar中的按钮:重写主题中的actionMenuTextColor。

为Android 3.0及以上版本定义

当仅仅支持了Android 3.0及更高的版本,你的XML风格文件应该使这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.Holo"><item name="android:actionBarStyle">@style/MyActionBar</item><item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item><item name="android:actionMenuTextColor">@color/actionbar_text</item></style><!-- ActionBar styles --><style name="MyActionBar"parent="@style/Widget.Holo.ActionBar"><item name="android:titleTextStyle">@style/MyActionBarTitleText</item></style><!-- ActionBar title text --><style name="MyActionBarTitleText"parent="@style/TextAppearance.Holo.Widget.ActionBar.Title"><item name="android:textColor">@color/actionbar_text</item></style><!-- ActionBar tabs text styles --><style name="MyActionBarTabText"parent="@style/Widget.Holo.ActionBar.TabText"><item name="android:textColor">@color/actionbar_text</item></style>
</resources>

为Android 2.1及以上版本定义

当使用了支持库,你的XML风格文件应该使这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.AppCompat"><item name="android:actionBarStyle">@style/MyActionBar</item><item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item><item name="android:actionMenuTextColor">@color/actionbar_text</item><!-- Support library compatibility --><item name="actionBarStyle">@style/MyActionBar</item><item name="actionBarTabTextStyle">@style/MyActionBarTabText</item><item name="actionMenuTextColor">@color/actionbar_text</item></style><!-- ActionBar styles --><style name="MyActionBar"parent="@style/Widget.AppCompat.ActionBar"><item name="android:titleTextStyle">@style/MyActionBarTitleText</item><!-- Support library compatibility --><item name="titleTextStyle">@style/MyActionBarTitleText</item></style><!-- ActionBar title text --><style name="MyActionBarTitleText"parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"><item name="android:textColor">@color/actionbar_text</item><!-- The textColor property is backward compatible with the Support Library --></style><!-- ActionBar tabs text --><style name="MyActionBarTabText"parent="@style/Widget.AppCompat.ActionBar.TabText"><item name="android:textColor">@color/actionbar_text</item><!-- The textColor property is backward compatible with the Support Library --></style>
</resources>

自定义标签指示器

如果要改变导航标签navigation tabs的指示器的话,创建一个activity主题,然后重写actionBarTabStyle属性。这个属性会指向其它样式资源。这个样式资源需要重写background属性,然后并设置它的值为一个状态列表图像资源。

Note:状态列表图像是很重要的。它可以在标签选中的情况下使用户区别出选中的标签与未选中便签的不同,从而判断是哪个便签被选中。有关如何创建一个作用于展示多种按钮状态的图像资源,请参见State List文档。

举个例子,以下是一个状态列表图像资源文件的内容。这里声明了用作于ActionBar标签的若干个状态,每个状态都指定了一张背景图:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- STATES WHEN BUTTON IS NOT PRESSED --><!-- Non focused states --><item android:state_focused="false" android:state_selected="false"android:state_pressed="false"android:drawable="@drawable/tab_unselected" /><item android:state_focused="false" android:state_selected="true"android:state_pressed="false"android:drawable="@drawable/tab_selected" /><!-- Focused states (such as when focused with a d-pad or mouse hover) --><item android:state_focused="true" android:state_selected="false"android:state_pressed="false"android:drawable="@drawable/tab_unselected_focused" /><item android:state_focused="true" android:state_selected="true"android:state_pressed="false"android:drawable="@drawable/tab_selected_focused" />
<!-- STATES WHEN BUTTON IS PRESSED --><!-- Non focused states --><item android:state_focused="false" android:state_selected="false"android:state_pressed="true"android:drawable="@drawable/tab_unselected_pressed" /><item android:state_focused="false" android:state_selected="true"android:state_pressed="true"android:drawable="@drawable/tab_selected_pressed" /><!-- Focused states (such as when focused with a d-pad or mouse hover) --><item android:state_focused="true" android:state_selected="false"android:state_pressed="true"android:drawable="@drawable/tab_unselected_pressed" /><item android:state_focused="true" android:state_selected="true"android:state_pressed="true"android:drawable="@drawable/tab_selected_pressed" />
</selector>

为Android 3.0及以上版本定义

当仅仅支持了Android 3.0及更高的版本,你的XML风格文件应该使这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.Holo"><item name="android:actionBarTabStyle">@style/MyActionBarTabs</item></style><!-- ActionBar tabs styles --><style name="MyActionBarTabs"parent="@style/Widget.Holo.ActionBar.TabView"><!-- tab indicator --><item name="android:background">@drawable/actionbar_tab_indicator</item></style>
</resources>

为Android 2.1及以上版本定义

当使用了支持库,你的XML风格文件应该使这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- the theme applied to the application or activity --><style name="CustomActionBarTheme"parent="@style/Theme.AppCompat"><item name="android:actionBarTabStyle">@style/MyActionBarTabs</item><!-- Support library compatibility --><item name="actionBarTabStyle">@style/MyActionBarTabs</item></style><!-- ActionBar tabs styles --><style name="MyActionBarTabs"parent="@style/Widget.AppCompat.ActionBar.TabView"><!-- tab indicator --><item name="android:background">@drawable/actionbar_tab_indicator</item><!-- Support library compatibility --><item name="background">@drawable/actionbar_tab_indicator</item></style>
</resources>

更多资源

  • ActionBar指南列出了更多ActionBar的风格属性。
  • Styles and Themes指南可以学习到主题样式的工作原理。
  • 移步Android Action Bar Style Generator查看更多的ActionBar完整样式。

这篇关于Android官方开发文档Training系列课程中文版:添加ActionBar之自定义ActionBar样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013002

相关文章

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Python Pillow 库详解文档(最新推荐)

《PythonPillow库详解文档(最新推荐)》Pillow是Python中最流行的图像处理库,它是PythonImagingLibrary(PIL)的现代分支和继承者,本文给大家介绍Pytho... 目录python Pillow 库详解文档简介安装核心模块架构Image 模块 - 核心图像处理基本导入

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部