我正在使用最新的appcompat支持库实现棒棒糖风格的导航抽屉,但问题是汉堡包图标永远不会显示.仅显示后退图标.
这是我的活动代码
import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.View; public class Home extends ActionBarActivity { private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); initViews(); } private void initViews(){ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); setSupportActionBar(toolbar); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar , R.string.drawer_open, R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); //getActionBar().setTitle(mTitle); //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); //getActionBar().setTitle(mDrawerTitle); //invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } }
这是我的样式文件
布局文件
在这两种情况下只显示了后退箭头,我已阅读了很多帖子,但似乎没有任何区别.任何帮助,将不胜感激.
你需要打电话
mDrawerToggle.syncState();
确保您导入正确的抽屉切换.
当我导入v4版本时,我有箭头(下面).
import android.support.v4.app.ActionBarDrawerToggle;
将其更改为此(下面,v7)修复了我的问题.
import android.support.v7.app.ActionBarDrawerToggle;
确保你打电话
mDrawerToggle.syncState();
打电话之后
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
使用ActionBarDrawerToggle时,必须在onPostCreate()和onConfigurationChanged()期间调用它
@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); }
由于我的NavigationDrawer正在扩展Fragment而不是Activity,因此我无法覆盖postCreate.以下是我的所作所为.
ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); // this sets the button to the back icon actionBar.setHomeButtonEnabled(true); // makes it clickable actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);// set your own icon
希望能帮助到你!
您可以简单地使用:
// Defer code dependent on restoration of previous instance state. mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer); } });
不要忘记覆盖onOptionsItemSelected方法并检查是否单击了ctionBarDrawerToggle,在这种情况下返回true,否则活动将完成.
@Override public boolean onOptionsItemSelected(MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); }