How to change the humberger icon in toolbar?

前提是你 提交于 2021-01-28 19:51:47

问题


I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.

I have tried many times but I am unable to change the icon with my own icon .

Can anyone help me ?

new DrawerBuilder()
    .withActivity(this)
    .withTranslucentStatusBar(false)
    .withActionBarDrawerToggle(false)
    .withToolbar(toolbar)
    .addDrawerItems(
        //pass your items here
    )
    .build();

CODE TO SHOW THE HUMBURGER ICON:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);

following is the code I found many times but i tried this also but it did not work

Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);

        actionBar.setHomeAsUpIndicator(upArrow);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true); 

And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?


回答1:


I haven't tried it with that library but, try the following:

ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);

            actionBar.setHomeAsUpIndicator(upArrow);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }



回答2:


As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.

For that you can do some thing like that

protected void onCreate(Bundle savedInstanceState) {
        ...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
        toggle.setDrawerIndicatorEnabled(false);
        toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
        ...
    }

Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.

protected void onCreate(Bundle savedInstanceState) {
        ...
        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }
            }
        });
        ...
    }

You can update the icon dynamically whenever required as

toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);

Hope this will help you.




回答3:


private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;

@Override

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}

}

Again Do you have any quires get Consult me here.....,hope you got solution to your problem...




回答4:


Try this by modify following:

 result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);

to

 result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);

this disable library default icon then change the icon...

 getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);


来源:https://stackoverflow.com/questions/38673653/how-to-change-the-humberger-icon-in-toolbar

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