更新时间:2022-11-11 09:51:29
一个简单的问题,如何使用操作栏的getDisplayOptions()?
Simple question how do I use getDisplayOptions() of action bar?
无论何时更改ActionBar
中的显示选项,都会在内部调用ActionBar.setDisplayOptions(int options, int mask)
,以将该显示选项与相应的位掩码配对.
Whenever you change the display options in the ActionBar
, ActionBar.setDisplayOptions(int options, int mask)
is called internally to pair that display option with the corresponding bitmask.
例如,当调用ActionBar.setDisplayHomeAsUpEnabled
时,内部调用ActionBar.setDisplayOptions(int options, int mask)
是这样的:
For instance, when ActionBar.setDisplayHomeAsUpEnabled
is called, internally ActionBar.setDisplayOptions(int options, int mask)
is called like this:
setDisplayOptions(showHomeAsUp ? DISPLAY_HOME_AS_UP : 0, DISPLAY_HOME_AS_UP);
因此,如果要检查是否启用了特定的显示选项,只需使用按位操作将该掩码与0
进行比较即可.
So, if you want to check to see if particular display option is enabled, you just compare that mask to 0
using a bitwise operation.
// Retrieve the current set of display options
final int displayOptions = actionBar.getDisplayOptions();
// Determine which display options are enabled
final boolean isShowHomeEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0;
final boolean isHomeAsUpEnabled = (displayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
final boolean isShowTitleEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
final boolean isUseLogoEnabled = (displayOptions & ActionBar.DISPLAY_USE_LOGO) != 0;
final boolean isShowCustomEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0;
完整的示例可能类似于:
A complete example might be something like:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
// Retrieve the current set of display options
final int displayOptions = actionBar.getDisplayOptions();
// Determine which display options are enabled
final boolean isShowHomeEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_HOME) != 0;
final boolean isHomeAsUpEnabled = (displayOptions & ActionBar.DISPLAY_HOME_AS_UP) != 0;
final boolean isShowTitleEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
final boolean isUseLogoEnabled = (displayOptions & ActionBar.DISPLAY_USE_LOGO) != 0;
final boolean isShowCustomEnabled = (displayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0;
System.out.println("show home: " + isShowHomeEnabled);
System.out.println("home as up: " + isHomeAsUpEnabled);
System.out.println("show title: " + isShowTitleEnabled);
System.out.println("use logo: " + isUseLogoEnabled);
System.out.println("show custom: " + isShowCustomEnabled);
}
哪个会打印:
show home: false
home as up: true
show title: false
use logo: true
show custom: false