其他分享
首页 > 其他分享> > android – 更改导航抽屉汉堡包图标

android – 更改导航抽屉汉堡包图标

作者:互联网

我正在尝试更改NavigationView的汉堡包菜单图标,但我无法这样做.

这是我到目前为止所尝试的

我有一个基本活动,其中导航抽屉设置完成.这是相关的一段代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_base_nav);
    setSupportActionBar(toolbar);
    setupDrawer();
}

private void setupDrawer() {
    mDrawerLayout.setDrawerListener(this);

    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerToggle = new ActionBarDrawerToggle(mContext,
            mDrawerLayout,
            R.string.open,
            R.string.close);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
       mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x);
    }
    mDrawerToggle.syncState();

    mNavigationView.setNavigationItemSelectedListener(
            menuItem -> {
                mMenuItem = menuItem.getItemId();
                mDrawerUtil.onNavMenuItemClicked(mMenuItem);
                mDrawerLayout.closeDrawers();
                return true;
            });
}

@Override 
public void setContentView(int layoutResID) {
    getLayoutInflater().inflate(layoutResID, mContainer);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

但它似乎对我不起作用.我也试过在SupportActionBar上调用setDrawerIndicatorEnabled(false)和setHomeAsUpIndicator(R.drawable.ic_share_48pt_2x),但这也行不通.

解决方法:

以下代码对我很有用,

protected void onCreate(Bundle savedInstanceState) {
    ...
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
    toggle.setDrawerIndicatorEnabled(false);
    toggle.setHomeAsUpIndicator(R.drawable.ic_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);
            }
        }
    });
    ...
}

最后,我可以动态更新图标

toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);

标签:hamburger-menu,android,android-toolbar,navigation-drawer,actionbardrawertoggle
来源: https://codeday.me/bug/20191004/1853147.html