Changing the Action bar icon

前端 未结 7 1544
栀梦
栀梦 2020-12-10 15:44

I\'m currently implementing theme support for my application and a part of it is changing the action bar app icon. I want to use a dark icon when Holo Light is selected. Eve

相关标签:
7条回答
  • 2020-12-10 16:17

    Calling to setIcon wasn't enough for me.

    Before that, I had to switch the display from activity logo to activity icon:

    actionBar.setDisplayUseLogoEnabled(false);
    

    For the differences between activity icon and logo see Android icon vs logo.

    0 讨论(0)
  • 2020-12-10 16:29

    I am using this for my use , and it's working for me. Hope this help all

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeAsUpIndicator(R.drawable.icon);
    
    0 讨论(0)
  • 2020-12-10 16:31

    Though its a bit late answer but i thought it might be useful.

    From inside an activity: For API level 14 or higher:

    getActionBar().setIcon(R.drawable.my_icon);
    

    For lower API level we have to extend ActionBarActivity and then:

    getSupportActionBar().setIcon(R.drawable.my_icon);
    

    From inside a Fragment: For API level 14 or higher:

    getActivity().getActionBar().setIcon(R.drawable.my_icon);
    

    For lower API level we can use (activity must extend ActionBarActivity):

    ((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);
    

    And in both cases we have to call setDisplayShowHomeEnabled(true) before setting the icon or logo.

    ((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
    
    ((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);
    
    0 讨论(0)
  • 2020-12-10 16:36

    You need to add the drawable that you want to reference into your drawable/ folder under res/.

    edit: In your Android installation folder there are a lot of stock images to use. You can probably find it there.

    0 讨论(0)
  • 2020-12-10 16:41
    getActionBar();
    

    You're throwing the action bar away right there. getActionBar() returns an instance of ActionBar, which you then need to call setIcon() on. Like so:

    ActionBar actionBar = getActionBar();
    actionBar.setIcon(R.drawable.my_icon);
    
    0 讨论(0)
  • 2020-12-10 16:42

    Try this

        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null)
        getSupportActionBar().setIcon(R.drawable.your_icon);
    
    0 讨论(0)
提交回复
热议问题