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完整样式。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/479347.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

美团 iOS 工程 zsource 命令背后的那些事儿

zsource 命令是什么&#xff1f; 美团 App 在 2015 年就已经基于 CocoaPods 完成了组件化的工作。在组件化的改造过程中&#xff0c;为了能够加速整体工程的构建速度&#xff0c;我们对需要集成进美团 App 的组件进行了二进制化&#xff0c;同时提供一个叫做 cocoapods-binary …

互联网大厂CTR预估前沿进展

文 | Ruhjkg编 | 小鹿鹿lulu源 | 知乎前言CTR&#xff08;click through rate&#xff09;预估模型是广告推荐领域的核心问题。早期主要是使用LR&#xff08;线性回归&#xff09;人工特征工程的机器学习方法&#xff0c;但是存在人工组合特征工程成本较高&#xff0c;不同任务…

以史为鉴 | 为什么要将「知识图谱」追溯到1956年?

本文转载自公众号&#xff1a;AI科技评论。作者 | Claudio Gutierrez 编译 | MrBear编辑 | Tokai以史为鉴&#xff0c;可以知兴替。纵观近期包括 AAAI、NeurIPS、IJCAI 在内的AI顶级会议&#xff0c;对图结构模型的研究是一个绕不开的话题&#xff0c;大量学者涌入这个赛道&…

Android官方开发文档Training系列课程中文版:添加ActionBar之ActionBar浮层效果

原文地址 : http://android.xsoftlab.net/training/basics/actionbar/overlaying.html 浮层效果的ActionBar 默认情况下&#xff0c;ActionBar总是会出现在Activity窗口的顶部&#xff0c;这样会稍微的减少Activity布局的剩余空间。如果需要在用户使用的时候隐藏和显示Action…

美团大规模微服务通信框架及治理体系OCTO核心组件开源

微服务通信框架及治理平台OCTO作为美团基础架构设施的重要组成部分&#xff0c;目前已广泛应用于公司技术线&#xff0c;稳定承载上万应用、日均支撑千亿级的调用。业务基于OCTO提供的标准化技术方案&#xff0c;能够轻松实现服务注册/发现、负载均衡、容错处理、降级熔断、灰度…

领域应用 | 知识结构化在阿里小蜜中的应用

本文转载自公众号&#xff1a;DataFunTalk。分享嘉宾&#xff1a;李凤麟 阿里巴巴 算法专家文章整理&#xff1a;付一韬内容来源&#xff1a;2019知识图谱前沿技术论坛出品社区&#xff1a;DataFun导读&#xff1a;阿里小蜜是阿里巴巴服务领域的重要人工智能产品&#xff0c;是…

内卷的世界,我们是否可以换一种思维生活?

文 | Flood Sung源 | 知乎前言今年最热门的词汇之一当属内卷了。似乎很多行业都由于份额有限而陷入内卷当中。最火的或许是清华学生的这张图&#xff0c;“骑车写代码”&#xff1a;图片来自网络虽然后来知道是这位同学怕关了屏幕程序就断了&#xff0c;但这不禁让人思考&#…

LeetCode 513. 找树左下角的值(按层遍历 queue)

1. 题目 给定一个二叉树&#xff0c;在树的最后一行找到最左边的值。 2. 解题 利用队列按层次遍历顺序&#xff0c;根右左&#xff0c;要求最左边的一个&#xff0c;所以根右左&#xff0c;最后一个队列元素就是答案 class Solution { public:int findBottomLeftValue(TreeN…

Hadoop YARN:调度性能优化实践

背景 YARN作为Hadoop的资源管理系统&#xff0c;负责Hadoop集群上计算资源的管理和作业调度。 美团的YARN以社区2.7.1版本为基础构建分支。目前在YARN上支撑离线业务、实时业务以及机器学习业务。 离线业务主要运行的是Hive on MapReduce&#xff0c; Spark SQL为主的数据仓库作…

LeetCode 39. 组合总和(排列组合 回溯)

1. 题目 给定一个无重复元素的数组 candidates 和一个目标数 target &#xff0c;找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。 说明&#xff1a; 所有数字&#xff08;包括 target&#xff09;都是正整数。 解集不能包…

深度学习平台的未来:谁会赢得下半场?

今天这篇文章无意引战&#xff0c;只想从历史发展的角度来谈谈深度学习大背景下的开发工具变迁&#xff0c;以及对未来发展趋势的想象。TensorFlow&#xff1a;无力回天的深度学习里程碑不知道有多少小伙伴是2017年以前入坑深度学习的&#xff0c;那时候人工智能概念火热&#…

论文浅尝 | 基于属性嵌入的知识图谱实体对齐

论文笔记整理&#xff1a;王中昊&#xff0c;天津大学硕士&#xff0c;方向&#xff1a;自然语言处理。来源&#xff1a;AAAI2019论文链接&#xff1a; https://doi.org/10.1609/aaai.v33i01.3301297概述知识图谱之间的实体对齐的任务目标是去找到那些在两个不同的知识图谱上表…

基本功 | Litho的使用及原理剖析

1. 什么是Litho&#xff1f; Litho是Facebook推出的一套高效构建Android UI的声明式框架&#xff0c;主要目的是提升RecyclerView复杂列表的滑动性能和降低内存占用。下面是Litho官网的介绍&#xff1a; Litho is a declarative framework for building efficient user interfa…

论文浅尝 | 基于深度强化学习将图注意力机制融入知识图谱推理

论文笔记整理&#xff1a;陈名杨&#xff0c;浙江大学直博生。Introduction知识图谱&#xff08;KGs&#xff09;在很多NLP的下游应用中起着越来越重要的作用。但是知识图谱常常是不完整的&#xff0c;所以解决知识图谱补全的任务也非常重要。主要有三种方法来完成知识图谱补全…

聊聊如何提升推荐系统的结果多样性

文 | 洪九(李戈)源 | 知乎个性化推荐系统的出现为处理信息过载问题提供了一个有效的工具&#xff0c;已经成为互联网各大平台(电商、信息流等)的标配&#xff0c;并在技术(个性化召回、个性化排序等)上取得了长足的发展&#xff0c;逐渐从传统模型过度到深度学习时代。但是&…

论文浅尝 | GNN with Generated Parameters for Relation Extraction

论文笔记整理&#xff1a;申时荣&#xff0c;东南大学博士生。地址&#xff1a;https://arxiv.org/pdf/1902.00756.pdf来源&#xff1a;ACL2019在许多自然语言处理任务&#xff08;例如关系提取&#xff09;中&#xff0c;多跳关系推理是必不可少的&#xff0c;而图神经网络&am…

大众点评信息流基于文本生成的创意优化实践

1. 引言 信息流是目前大众点评除搜索之外的第二大用户获取信息的入口&#xff0c;以优质内容来辅助用户消费决策并引导发现品质生活。整个大众点评信息流&#xff08;下文简称点评信息流&#xff09;围绕个性化推荐去连接用户和信息&#xff0c;把更好的内容推荐给需要的用户。…

LeetCode 701. 二叉搜索树中的插入操作(二叉查找树/插入)

1. 题目 给定二叉搜索树&#xff08;BST&#xff09;的根节点和要插入树中的值&#xff0c;将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。 注意&#xff0c;可能存在多种有效的插入方式&#xff0c;只要树在插入后仍保持为二叉搜索…

docker的简单操作和端口映射

docker的简单操作和端口映射&#xff1a;https://www.cnblogs.com/lixaingyang/p/11976827.html docker的简单操作和端口映射 一&#xff1a;简介 Docker镜像 在Docker中容器是基于镜像启动的 镜像是启动容器的核心 镜像采用分层设计&#xff0c;最顶层为读写层 使用快照COW技…

Android官方开发文档Training系列课程中文版:管理Activity的生命周期之启动一个Activity

原文地址 : http://android.xsoftlab.net/training/basics/activity-lifecycle/index.html 导言 用户通过导航退出或者返回应用的时候&#xff0c;应用中Activity的生命周期会在不同的状态之间变换。举个例子&#xff0c;当Activity初次启动的时候&#xff0c;它会来到系统的…