gpt4 book ai didi

android - 为什么单击我的导航图标时我的 NavDrawer 没有打开?

转载 作者:行者123 更新时间:2023-11-30 02:11:09 25 4
gpt4 key购买 nike

我使用自定义导航图标,所以我需要 drawerToggle.setDrawerIndicatorEnabled(false);。但是现在,单击我的自定义图标时,我的 Navdrawer 不会打开。

有什么想法可以实现吗?我还需要 ActionBarDrawerToggle 吗?

public void setUpActionBar() {
actionBar = (Toolbar) findViewById(R.id.custom_screen_toolbar);
setSupportActionBar(actionBar);

actionBar.setBackgroundResource(R.drawable.divider_action_bar);
actionBar.setNavigationIcon(R.drawable.action_bar_menu);

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

drawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
actionBar, /* custom action bar */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, 0);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};

drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.syncState();
}

我还尝试使用 drawerToggle.setHomeAsUpIndicator(R.drawable.icon); 这样我就可以在不使用操作栏中的 setNavigationIcon 的情况下更改图标,但是它不会更改图标。

最佳答案

如果您调用 drawerToggle.setHomeAsUpIndicator(R.drawable.icon);drawerToggle.setDrawerIndicatorEnabled(false); 然后它会运行

public void setHomeAsUpIndicator(Drawable indicator) {
if(indicator == null) {
this.mHomeAsUpIndicator = this.getThemeUpIndicator();
this.mHasCustomUpIndicator = false;
} else {
this.mHomeAsUpIndicator = indicator;
this.mHasCustomUpIndicator = true;
}

if(!this.mDrawerIndicatorEnabled) {
this.setActionBarUpIndicator(this.mHomeAsUpIndicator, 0);
}

public void setDrawerIndicatorEnabled(boolean enable) {
if(enable != this.mDrawerIndicatorEnabled) {//if you set enable be "false", below sentences do not run, because the default value of mDrawerIndicatorEnabled is false
if(enable) {
this.setActionBarUpIndicator((Drawable)this.mSlider, this.mDrawerLayout.isDrawerOpen(8388611)?this.mCloseDrawerContentDescRes:this.mOpenDrawerContentDescRes);
} else {
this.setActionBarUpIndicator(this.mHomeAsUpIndicator, 0);
}

this.mDrawerIndicatorEnabled = enable;
}

}

虽然你调用了setHomeAsUpIndicator(Drawable indicator),但是因为mDrawerIndicatorEnabled为false,所以它不会改变图标。(但是如果你先调用setDrawerIndicatorEnabled再调用setHomeAsUpIndicator 你也可以改变图标)。

如果你调用toolbar.setNavigationIcon(R.mipmap.ic_launcher);,图标会改变,因为它会调用ActionBarDrawerToggle.java中的setActionBarUpIndicator 。您将看到工具栏将为其导航图标设置此可绘制对象。

public void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes) {
this.mToolbar.setNavigationIcon(upDrawable);
this.mToolbar.setNavigationContentDescription(contentDescRes);
}

如果你想通过点击打开抽屉菜单,你应该为这个'navigationIcon'设置一个点击监听器,因为在你调用这个之后,系统不会帮你做这个。

关于android - 为什么单击我的导航图标时我的 NavDrawer 没有打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com