dynamically enable/disable hide/unhide Android Act

2020-07-06 06:42发布

I am developing an Android application that employs a standard ActionBar.
on a certain screen i have a Filter icon that is conditionally required,
depending on characteristics of the data being displayed, e.g. some data is filterable
Others not.
i cannot find any methods on actionbar that look likely candidates for programmaticaly hiding an actionbar action icon. how can i enable/disable an actionbar action icon?
Or
how can i hide/unhide an actionbar action icon?

5条回答
我只想做你的唯一
2楼-- · 2020-07-06 07:23

Try ActionBar.getActionBar.hide();

查看更多
欢心
3楼-- · 2020-07-06 07:25

You can do it by overriding the onPrepareOptionsMenu() method. Here is a small example

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (count < 1)
        menu.getItem(4).setEnabled(false); //disable menuitem 5
    if (!after)
    menu.getItem(1).setVisible(false); // invisible menuitem 2
    invalidateOptionsMenu();
    return true;
}

However, This method is only called whenever you click the menu button in the action bar. If you've any icons on the action bar (except menu button), clicking on that will not trigger the OnPrepareOptionsMenu() method. In order to trigger this method manually you can use the invalidateOptionsMenu() in your methods. like this

void yourMethod () {
...
invalidateOptionsMenu();
...
}
查看更多
混吃等死
4楼-- · 2020-07-06 07:27

As I understand, you need to remove the icon that shows on the action bar top left.

You can do that with this simple line of code:

final ActionBar actionBar = getActionBar();     
actionBar.setDisplayShowTitleEnabled(false);

And if you like to get rid of the app name try:

actionBar.setDisplayShowHomeEnabled(false);

Hope this helps.

查看更多
爷、活的狠高调
5楼-- · 2020-07-06 07:30

You can also try this:

final ActionBar actionBar= getActionBar();
actionBar.hide();

It will hide the icon and title.

Don't forget your import:

import android.app.ActionBar;

Good Luck

查看更多
相关推荐>>
6楼-- · 2020-07-06 07:48

Have a look at this, it shows how to change menu items at runtime.

http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

You could for example save the menu as a member variable of your Activity inside onCreateOpionsMenu() and then do something like this:

MenuItem item = mMenu.findItem(R.id.addAction);
item.doSomething()

when you want to change something on a specific menu item.

查看更多
登录 后发表回答