Android ActionBar setDisplayOptions 使用详解


先来看看官方文档的说明

setDisplayOptions

setDisplayOptions Added in API level 11


ActionBar 提供了下面几种模式,来设置导航栏的样式

Constants
int DISPLAY_HOME_AS_UP Display the ‘home’ element such that it appears as an ‘up’ affordance. //返回图标
int DISPLAY_SHOW_CUSTOM Show the custom view if one has been set. // 自定义View
int DISPLAY_SHOW_HOME Show ‘home’ elements in this action bar, leaving more space for other navigation elements.//显示“主页”元素,为其他导航元素留出更多空间。这包括Icon和Logo
int DISPLAY_SHOW_TITLE Show the activity title and subtitle, if present. //显示标题和副标题(如果有)
int DISPLAY_USE_LOGO Use logo instead of icon if available. // 显示Logo
int NAVIGATION_MODE_LIST This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead. //菜单列表取代静态标题,已经弃用
int NAVIGATION_MODE_STANDARD This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.//标准模式,已经弃用
int NAVIGATION_MODE_TABS This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead. //标签导航模式。该模式代替静态标题文本,而是提供一系列选项卡,已经弃用

如何设置ActionBar的样式,即通过setDisplayOptions 方法

  • public abstract void setDisplayOptions (int options)

Set display options. This changes all display option bits at once. To change a limited subset of display options, see setDisplayOptions(int, int).

Parameters
options
int: A combination of the bits defined by the DISPLAY_ constants defined in ActionBar. Value is either 0 or a combination of DISPLAY_USE_LOGO, DISPLAY_SHOW_HOME, DISPLAY_HOME_AS_UP, DISPLAY_SHOW_TITLE, DISPLAY_SHOW_CUSTOM, and android.app.ActionBar.DISPLAY_TITLE_MULTIPLE_LINES

一次性设置显示的选项,如果只设置部分选项,则使用下面这个方法


  • public abstract void setDisplayOptions (int options, int mask)

Set selected display options. Only the options specified by mask will be changed. To change all display option bits at once, see setDisplayOptions(int).

Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the DISPLAY_SHOW_HOME option. setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO) will enable DISPLAY_SHOW_HOME and disable DISPLAY_USE_LOGO.

Parameters
options
A combination of the bits defined by the DISPLAY_ constants defined in ActionBar.
mask
A bit mask declaring which display options should be changed.
选择部分来显示设置,仅仅有当options定义的选项,在mask中被设置才干被显示。也就是设置为true。


具体使用

  • 1.默认用法

ActionBar默认会显示出一个箭头(DISPLAY_HOME_AS_UP),一个logo(DISPLAY_SHOW_HOME),标题(DISPLAY_SHOW_TITLE)

  • 2.使用setDisplayOptions(int options)方法

首先所有的项目都变成了false,然后options使用或运算添加设置,添加一个就设置一个为true-显示,比如setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE),就是设置显示返回箭头和标题

  • 3.使用setDisplayOptions(int options,int mask)方法

首先所有的项目都变成了false,然后options使用与运算添加设置,比如setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM)
mask和options中都有ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE,那么就是把箭头和标题设置为显示,mask中剩下的ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM 就设置为隐藏,其他没有提到的选项就应该为默认的配置。
可以这样理解,mask和option执行了&运算,同时出现的为true,出现一个的为false。

  • 4.使用setDisplayHomeAsUpEnabled、setDisplayShowCustomEnabled等方法,设置单个选项

例如使用setDisplayHomeAsUpEnabled(true),setDisplayShowCustomEnabled(true)等单个设置;则等同于使用方法2 setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM)
如果使用setDisplayHomeAsUpEnabled(true),setDisplayShowCustomEnabled(false),等设置,则等同于使用方法3
setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_HOME_AS_UP)


参考使用:
https://blog.csdn.net/mobilexu/article/details/41147417

更多相关文章

  1. Android(安卓)MediaPlayer研究问题
  2. Android中自定义DatePicker
  3. Android(安卓)动态获取创建与删除文件权限
  4. php、java、android、ios通用的3des方法(推荐)
  5. 保存Activity状态
  6. android v7支持包导入问题,Support Error的正确解决方法
  7. Android(安卓)sdk manager更新 下载API源码
  8. Android(安卓)UI系列-----CheckBox和RadioButton(1)
  9. android xutils

随机推荐

  1. Android intent 传递对象以及返回刷新
  2. android WebView将新浪天气为我所用 ----
  3. 说一下你眼中的Android的优点和不足之处(
  4. Android的IPC机制(七)—— Socket的原理简
  5. 【Android笔记】ubuntu 16.04下android调
  6. Android(安卓)IntentService完全解析 当S
  7. Android开发——Accessibility机制实现模
  8. Android 框架问题分析案例 - 谁杀了桌面?
  9. Android下Activity结束(finish())之后没有
  10. Android项目中添加广告