How do I get ActionBar attribute in my class [android]

会有一股神秘感。 提交于 2020-01-03 18:52:06

问题


Simple question how do I use getDisplayOptions() of action bar? I want to maintain the state of action bar as I am flipping my view with some custom view. So before bringing my view I am storing my action bar state, so that I revert back to original state when my view is removed. I set few setting to my action bar like

    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayShowCustomEnabled(false);
    actionBar.setDisplayShowHomeEnabled(true);

So when I remove my view I want to revert back these setting. But I don't see any method like actionBar.isDisplayHomeAsUpEnabled() etc in API. Although I see actionBar.getDisplayOptions(). But don't know how to use it. Can any body help me achieving this.?


回答1:


Simple question how do I use getDisplayOptions() of action bar?

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.

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);

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);
}

Which would print:

show home: false
home as up: true
show title: false
use logo: true
show custom: false


来源:https://stackoverflow.com/questions/26794590/how-do-i-get-actionbar-attribute-in-my-class-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!