为什么单击我的导航图标时我的 NavDrawer 没有打开?
Why doesn't my NavDrawer open when clicking my navigation icon?
我使用自定义导航图标,所以我需要 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
的情况下更改图标,但它不会更改图标。
setDrawerIndicatorEnabled(false);
将禁用抽屉指示器,所以我猜它需要设置为 true。此外,如果您需要抽屉的自定义图标,您可以将其设置为,
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.earth, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
)
drawable 参数应替换向上插入符号。
有用 link 下载示例。
如果你调用 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',因为你调用这个后,系统将不会帮助你。
我使用自定义导航图标,所以我需要 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
的情况下更改图标,但它不会更改图标。
setDrawerIndicatorEnabled(false);
将禁用抽屉指示器,所以我猜它需要设置为 true。此外,如果您需要抽屉的自定义图标,您可以将其设置为,
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.earth, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
)
drawable 参数应替换向上插入符号。
有用 link 下载示例。
如果你调用 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',因为你调用这个后,系统将不会帮助你。