Android 为什么';单击我的导航图标时,我的导航抽屉是否打开?
我使用自定义导航图标,因此需要Android 为什么';单击我的导航图标时,我的导航抽屉是否打开?,android,toolbar,Android,Toolbar,我使用自定义导航图标,因此需要drawerToggle.setDrawerIndicatorEnabled(false)。但现在,单击自定义图标时,我的Navdrawer不会打开 有没有办法做到这一点?我还需要一个ActionBarDrawertogle吗 public void setUpActionBar() { actionBar = (Toolbar) findViewById(R.id.custom_screen_toolbar); setSupportActionBa
drawerToggle.setDrawerIndicatorEnabled(false)代码>。但现在,单击自定义图标时,我的Navdrawer不会打开
有没有办法做到这一点?我还需要一个ActionBarDrawertogle吗
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
的情况下更改te图标,但它不会更改图标。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参数应替换向上插入符号
下载示例非常有用。如果调用抽屉切换.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);
}
及
尽管您调用setHomeAsUpIndicator(可绘制指示器),但由于mDrawerIndicatorEnabled为false,因此它不会更改图标。(但如果您先调用setDrawerIndicatorEnabled
,然后调用setHomeAsUpIndicator
,您也可以更改图标)
如果调用toolbar.setNavigationIcon(R.mipmap.ic_启动器)
,图标将更改,因为它将调用ActionBarDrawerToggle.java
中的setActionBarUpIndicator
。您将看到工具栏将为其导航图标设置此可绘制图标
public void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes) {
this.mToolbar.setNavigationIcon(upDrawable);
this.mToolbar.setNavigationContentDescription(contentDescRes);
}
如果您想通过单击打开抽屉菜单,您应该为此“导航图标”设置一个单击侦听器,因为在您调用此图标后,系统将不会为此提供帮助。我正在使用import android.support.v7.app.ActionBarDrawerToggle代码>你的解决方案仍然可行吗?它说它是未定义的。构造函数未定义,请使用支持库尝试此操作!这不适用于SupportV7,因为它需要一个工具栏
public void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes) {
this.mToolbar.setNavigationIcon(upDrawable);
this.mToolbar.setNavigationContentDescription(contentDescRes);
}