怎么设置android中actionbardrawertoggle的返回按钮
发布网友
发布时间:2022-04-22 16:08
我来回答
共1个回答
热心网友
时间:2022-04-12 17:34
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
void init() {
// 添加菜单
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
//actionBar.setDisplayShowTitleEnabled(false);//去掉标题
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(getResources().getString(R.string.app_title));
//actionBar.setLogo(R.drawable.icon_menu);
mDrawerLayout.setDrawerListener(new MyDrawerListener());//设置drawer的开关监听
mDrawerToggle = new ActionBarDrawerToggle(activity, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
}
/** activity创建完成后 */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();//该方法会自动和actionBar关联, 将开关的图片显示在了action上,如果不设置,也可以有抽屉的效果,不过是默认的图标
}
/** 菜单键点击的事件处理 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
/** 设备配置改变时 */
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/** drawer的监听 */
private class MyDrawerListener implements DrawerLayout.DrawerListener {
@Override
public void onDrawerOpened(View drawerView) {// 打开drawer
mDrawerToggle.onDrawerOpened(drawerView);//开关状态改为opened
}
@Override
public void onDrawerClosed(View drawerView) {// 关闭drawer
mDrawerToggle.onDrawerClosed(drawerView);//开关状态改为closed
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {// drawer滑动的回调
mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {// drawer状态改变的回调
mDrawerToggle.onDrawerStateChanged(newState);
}
}