How to change HomeAsUp indicator in new AppCompat Toolbar?

后端 未结 6 1735
既然无缘
既然无缘 2020-12-28 15:01

I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.

styles (same for other API\

相关标签:
6条回答
  • 2020-12-28 15:18

    Just add this line to your styles.xml

            <item name="navigationIcon">@drawable/ic_back_arrow</item>
    
    0 讨论(0)
  • 2020-12-28 15:22

    i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.

      @Override
       protected void onCreate(Bundle savedInstanceState) {
    
       .....
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_action_back);
    

    Post the following code in onOptionsItemSelected(MenuItem item)

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       ......
        if (id == android.R.id.home) {
            startActivity(new Intent(this, MainActivity.class));
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps

    0 讨论(0)
  • 2020-12-28 15:30

    To change icon just call at runtime:

    toolbar.setNavigationIcon(R.drawable.home);
    

    Trick with styles/themes not working.

    How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21

    0 讨论(0)
  • 2020-12-28 15:30

    Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    

    (as observed on Android 4.0.4, 4.3, and 4.4.2 phones)

    0 讨论(0)
  • 2020-12-28 15:34

    I tried setHomeAsUpIndicator(int resId) ,it works.

    private void initToolbar ( Toolbar mToolbar ) {
    
        mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
        setSupportActionBar ( mToolbar );
    
        ActionBar actionbar = getSupportActionBar ();
        actionbar.setDisplayHomeAsUpEnabled ( true );
        actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
    }
    

    But mToolbar.setNavigationIcon(R.drawable.ic_action_back) doesn't work :(

    0 讨论(0)
  • 2020-12-28 15:40

    If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:

    //set home as up indicator
    mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);
    
    //remove home as up indicator
    mDrawerToggle.setHomeAsUpIndicator(null);
    

    to show homeAsUpIndicator indicator instead of home indicator do following:

    mDrawerToggle.setDrawerIndicatorEnabled(false);
    

    Docs:

    ActionBarDrawerToggle#setHomeAsUpIndicator ActionBarDrawerToggle#setDrawerIndicatorEnabled

    0 讨论(0)
提交回复
热议问题