如何使导航抽屉切换按钮在工具栏上可见?

发布于 2025-01-13 17:59:51 字数 1324 浏览 2 评论 0原文

我收到错误消息ActionBarDrawerToggle:DrawerToggle 可能不会显示,因为 NavigationIcon 不可见。您可能需要调用actionbar.setDisplayHomeAsUpEnabled(true);

但我在我的代码中调用了这个,仍然遇到相同的错误,并且切换按钮不可见。

我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //layout variables
    mDrawerLayout = findViewById(R.id.main_drawer_layout);
    mNavigationView = findViewById(R.id.main_nav_view);


    mActionDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mActionDrawerToggle);
    mActionDrawerToggle.syncState();

    //changed as keeps throws NULLPOINTEXCEPTION
    if(getSupportActionBar() != null) {
       // getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // assigning ID of the toolbar to a variable
         toolbar = (Toolbar) findViewById(R.id.my_toolbar);

        // using toolbar as ActionBar
        setSupportActionBar(toolbar); //set


        ActionBar actionbar = getSupportActionBar(); //get
        actionbar.setTitle("SharePixel");
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setHomeAsUpIndicator(R.drawable.ic_home_menu);


    }

I am getting error message : ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);

But I have called this in my code , still getting same error and the toggle button is not visible.

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //layout variables
    mDrawerLayout = findViewById(R.id.main_drawer_layout);
    mNavigationView = findViewById(R.id.main_nav_view);


    mActionDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mActionDrawerToggle);
    mActionDrawerToggle.syncState();

    //changed as keeps throws NULLPOINTEXCEPTION
    if(getSupportActionBar() != null) {
       // getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // assigning ID of the toolbar to a variable
         toolbar = (Toolbar) findViewById(R.id.my_toolbar);

        // using toolbar as ActionBar
        setSupportActionBar(toolbar); //set


        ActionBar actionbar = getSupportActionBar(); //get
        actionbar.setTitle("SharePixel");
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setHomeAsUpIndicator(R.drawable.ic_home_menu);


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2025-01-20 17:59:51

setHomeAsUpIndicator() 不会执行,因为 if(getSupportActionBar() != null) 条件在基于抽屉的活动中不应有效,因为它需要自定义ToolBar,并且您在设置自定义工具栏之前调用此条件。

除非您的活动主题在样式/主题 XML 中设置了 ActionBar,否则无法实现此条件。 (如果您没有自定义活动主题集,则它是从基本应用程序主题继承的。

要解决此问题:

  1. 请确保您的活动主题继承自 NoActionBar 后代;例如 Theme。 MaterialComponents.DayNight.NoActionBar (或者如果您的 Activity 没有自定义样式;请在基本应用程序主题上执行此操作)
  2. 。 null) 条件。

setHomeAsUpIndicator() won't get executed as the if(getSupportActionBar() != null) condition shouldn't be valid in drawer-based activity, because it requires a custom Toolbar, and you call this condition before setting the custom toolBar.

This condition won't be achieved unless your activity theme has an ActionBar set in your styles/themes XML. (if you've no customized activity theme set, then it's inherited from the base application theme.

To fix this:

  1. Make sure that your activity theme inherits from a NoActionBar decedent; for instance Theme.MaterialComponents.DayNight.NoActionBar (or if your activity doesn't have customized style; do that on the base application theme).
  2. Remove the if(getSupportActionBar() != null) condition.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文