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

相关文章

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

如何基于Python开发一个微信自动化工具

《如何基于Python开发一个微信自动化工具》在当今数字化办公场景中,自动化工具已成为提升工作效率的利器,本文将深入剖析一个基于Python的微信自动化工具开发全过程,有需要的小伙伴可以了解下... 目录概述功能全景1. 核心功能模块2. 特色功能效果展示1. 主界面概览2. 定时任务配置3. 操作日志演示

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

ubuntu系统使用官方操作命令升级Dify指南

《ubuntu系统使用官方操作命令升级Dify指南》Dify支持自动化执行、日志记录和结果管理,适用于数据处理、模型训练和部署等场景,今天我们就来看看ubuntu系统中使用官方操作命令升级Dify的方... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四