Android setDisplayOptions 具体的使用说明

  Android有几个地方使用位计算。实例Intent Flags,它们的定义View onMeasure(int widthMeasureSpec, int heightMeasureSpec),并且ActionBar setDisplayOptions ,以下就理解一下setDisplayOptions的使用方法。

  先看下文档中对他的描写叙述:

public abstract void setDisplayOptions (int options)

Added in API level 11

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 A combination of the bits defined by the DISPLAY_ constants defined in ActionBar.


一次性设置选项这种方法。假设要设置部分。那么使用以下的方法

public abstract void setDisplayOptions (int options, int mask)

Added in API level 11

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。

那么问题来了。。。


究竟怎么用options 和mask尼?看以下内容

  • ActionBar默认假设没有做不论什么设置,会显示出一个箭头(DISPLAY_HOME_AS_UP),一个logo(DISPLAY_SHOW_HOME),标题(DISPLAY_SHOW_TITLE)

  • 一旦使用setDisplayOptions(int options)这种方法,全部的设置项都变成了false,options使用或运算加入设置。加入一个就设置一个为true-显示,比方setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM),就是设置显示返回箭头和customView

  • setDisplayOptions(int options,int mask)方法,即在当前默认的ActionBar设置项内。选择几个来设置。这种场景可能是Activity内部的Fragment。须要给Fragment设置ActionBar的几个确定选项,比方
 getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
options为 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM
mask为 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_CUSTOM。那么就是把箭头和customView设置为显示,mask中剩下的ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME 就设置为隐藏。即相应的setDisplayShowTitleEnabled(false),其它没有提到的选项就应该为默认的配置。


能够这样理解。mask和option运行了&运算,同一时候出现的为true,出现一个的为false。


知道了原理我们就能够这样用,比方我们仅仅要显示a,b,c三个选项,

那么就使用getSupportActionBar().setDisplayOptions( a|b|c );


假设我们在特定的条件下,比方fragment中,须要仅仅对b,c,d这三个选项进行设置,把b。c设置为显示。d设置为隐藏,那么就能够使用getSupportActionBar().setDisplayOptions(b|c , b|c|d );


用了这种方法就不须要一个一个的设置setDisplayShowXXX()了


了解很多其它能够去:http://blog.csdn.net/zzp16/article/details/7956768








版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/yxwkf/p/4813028.html