【问题标题】:Navigation drawer hamburguer icon doesn't show until swipe drawer manually在手动滑动抽屉之前,导航抽屉汉堡图标不会显示
【发布时间】:2015-12-15 18:28:09
【问题描述】:

我做了一些搜索并尝试了很多解决方案,但我会遇到同样的问题:

在我滑动抽屉之前,抽屉汉堡图标不会显示。

这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    setSupportActionBar(toolbar);
    setupDrawer();
    ...
}

//calling the method bellow inside onCreate
public void setupDrawer(){
    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            syncState();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            syncState();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
    ActionBar actionBar =  getActionBar();

    if(actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setIcon(R.drawable.ic_launcher);
    }
}

我希望它显示出来。

【问题讨论】:

  • 您应该使用getSupportActionBar() 而不是getActionBar()(可能返回null)。
  • @Kernald 是的,它可以工作,现在显示图标,并且可能不会导致空指针异常。
  • 太好了,我在下面贴了更详细的解释。

标签: android android-layout navigation-drawer


【解决方案1】:

尝试通过 getSupportActionBar() 方法获取操作栏,而不是 getActionBar()。在您的 setupDrawer() 方法中:

mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
// here
//ActionBar actionBar =  getActionBar();
ActionBar actionBar =  getSupportActionBar();

【讨论】:

    【解决方案2】:

    由于您使用的是Toolbar,并且可能是ActionBarActivity,因此您没有内置的ActionBarActionBarActivity 提供了一个围绕 Toolbar 的包装器,暴露了常用的 ActionBar 方法(如 setDisplayShowHomeEnabled(boolean enabled))。但是要得到这个包装器,你必须调用getSupportActionBar() 而不是getActionBar()

    在您的代码中,它只是转换为:

    public void setupDrawer(){
        // ...
    
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        drawerLayout.setDrawerListener(mDrawerToggle);
        ActionBar actionBar =  getSupportActionBar();
    
        if(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setIcon(R.drawable.ic_launcher);
        }
    }

    【讨论】:

    • 感谢您的回答,但它显示后退箭头,我想显示汉堡包图标,但我使用此解决方案修复了它 > stackoverflow.com/a/28136349/1879661,但不确定是否是最佳解决方案,什么你觉得呢?
    【解决方案3】:

    从@BahodirovMirjalol 解决方案修复后:

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
    ActionBar actionBar =  getSupportActionBar();
    

    我遇到了另一个问题,因为 导航返回图标显示而不是汉堡包,所以我在 onCreate 方法上方添加了这两个方法并且它有效:

    @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);
    }
    

    找到here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2022-08-15
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多