【问题标题】:how to hide navigation drawer indicator while using appcompat21如何在使用 appcompat21 时隐藏导航抽屉指示器
【发布时间】:2015-03-02 04:39:15
【问题描述】:

我按照 Cris Banes 指南使用工具栏小部件作为 ActionBar。 在我的用例中,我需要在滑动到 ViewPager 中包含的另一个片段时隐藏 Activity 中的导航抽屉。 以前,我在使用 ActionBar Widget 隐藏导航抽屉时使用了以下属性。这似乎工作正常。 getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setHomeButtonEnabled(false);

虽然现在在使用时更改为 AppCompat21

getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);

这似乎没有隐藏actionBar中的导航抽屉。 我在这方面是否遗漏了任何帮助。

【问题讨论】:

    标签: android android-actionbar navigation-drawer android-appcompat


    【解决方案1】:
    toolbar.setNavigationIcon(null);
    

    它会隐藏导航图标,参考你可以查看这个answer

    【讨论】:

    • 谢谢,它似乎隐藏了导航图标,但我没有为 setIcon 使用自定义图标。那么我将如何恢复导航抽屉指示器。
    • 感谢它使用 toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    【解决方案2】:

    如果你在DrawerLayout 中使用Toolbar --> AppBarLayout

    然后是班级

    ActionBarDrawerToggle-->setDrawerIndicatorEnabled(false) 
    

    函数将使导航抽屉图标不可见。就像

    public class MainActivity extends AppCompatActivity
                    implements NavigationView.OnNavigationItemSelectedListener
    {
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
    //the below line of code will allow you to hide the nav drawer icon 
            toggle.setDrawerIndicatorEnabled(false);    
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    

    【讨论】:

    • 当我尝试这个时,我得到一个后退箭头图标,而不是汉堡包(3 行)图标。 :-(
    • ...哎呀。发生这种情况是因为我还有 actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); 。删除这些,它现在运行良好。 10分! :-)
    【解决方案3】:

    您的代码只有在您使用时才能工作:

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    

    getSupportActionBar().setHomeButtonEnabled(false);
    

    你也可以试试:

    toolbar.setNavigationIcon(null);
    

    【讨论】:

    • 感谢 toolbar.setNavi.. 似乎隐藏了它,但为了显示它回来我不得不使用。 toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    • @harshitpthk 我讨厌求助于这样的 hacky 解决方案,但有时也无济于事。
    • 只是给可能会追随的互联网航海者的一个说明......重要的是你使用 getSupportActionBar(),而不是 getActionBar() ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2023-04-07
    • 1970-01-01
    • 2015-12-11
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多