mMainBar = (Toolbar)this.findViewById(R.id.main_bar);
this.setSupportActionBar(mMainBar);
mDrawerLayout = (DrawerLayout)this.findViewById(R.id.main_drawer_layout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mMainBar, R.string.app_name, R.string.hello_world);
mDrawerLayout.setDrawerListener(mToggle);
注意:此时的ActionBarDrawerToggle不是v4包中的,而是android.support.v7.app.ActionBarDrawerToggle
还需要下面的代码才能实现效果
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mToggle.syncState(); //这一行必须在setDrawerListener()之前调用
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mToggle.onConfigurationChanged(newConfig);
}
注意:syncState()和setDrawerListener()的调用顺序,否则效果不会出现。重写的是onPostCreate(Bundle savedInstanceState),别重现成onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState),我之前就是重写错了方法导致效果没有出现。