how to hide up button in actionbar

2019-02-10 02:36发布

问题:

I would like to do an edit mode, in the style of the tablet gmail app. If the user presses the edit button on the actionbar, than I would like to show him/her an action view, that has a done button on the left side, and a delete button on the right.

I have an example that works without actionbarsherlock here: https://code.google.com/p/romannurik-code/source/browse/misc/donediscard

I would like to stick to actionbarsherlock, for compatibility reasons.

This is the way I solved it in onCreateOptionsMenu:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
getSupportActionBar().setIcon(R.drawable.white);

for (int i = 0; i < menu.size(); i++) {
    menu.getItem(i).setVisible(false); }

getSupportActionBar().setDisplayHomeAsUpEnabled(false); does nothing, so I had to set the home icon to a white 1x1 pixel drawable.

I also had to set the background color of the actionbar, as the actionview's background color. If I had not, than the 1x1 home icon would have padding around it, and the original background color would be visible around the white home button.

Anyone got a better solution for this?

edit: I also had to change the style:

<style name="Theme.Styled" parent="Theme.Sherlock.Light">
        <item name="android:homeAsUpIndicator">@drawable/white</item>
        <item name="homeAsUpIndicator">@drawable/white</item>
</style>

Also..settings android:homeAsUpIndicator increased my min api level from 8 to 11 which is also an issue.

回答1:

If you're on API level 14 or above and are not using ActionbarSherlock, this code in onCreateOptionsMenu will disable the up button, remove the left caret, and remove the icon:

ActionBar actionBar = getActionBar();
if (actionBar != null) {
    actionBar.setHomeButtonEnabled(false); // disable the button
    actionBar.setDisplayHomeAsUpEnabled(false); // remove the left caret
    actionBar.setDisplayShowHomeEnabled(false); // remove the icon
}


回答2:

You were almost there. To hide the icon/logo completely, use setDisplayShowHomeEnabled(false). The call you're using just removes the little arrow that indicates that the icon acts as an "up" button also.



回答3:

This worked for me, though its a slight change in the above mentioned solution

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(false); // disable the button
            actionBar.setDisplayHomeAsUpEnabled(false); // remove the left caret
            actionBar.setDisplayShowHomeEnabled(false); // remove the icon
        }


回答4:

Though it's an older post, I would like to share an answer which worked for me.

To hide the UP button in actionbar, use the following in OnCreateOptionsMenu:

if (getSupportActionBar() != 
           getSupportActionBar().hide();
       }

To remove the UP button in actionbar, use the following in OnCreateOptionsMenu:

if (getSupportActionBar() != 
           getSupportActionBar().setDisplayHomeAsUpEnabled(false);
       }

I hope it will help newbies.