如何使导航抽屉切换按钮在工具栏上可见?
我收到错误消息: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setHomeAsUpIndicator()
不会执行,因为if(getSupportActionBar() != null)
条件在基于抽屉的活动中不应有效,因为它需要自定义ToolBar,并且您在设置自定义工具栏之前调用此条件。除非您的活动主题在样式/主题 XML 中设置了
ActionBar
,否则无法实现此条件。 (如果您没有自定义活动主题集,则它是从基本应用程序主题继承的。要解决此问题:
NoActionBar
后代;例如Theme。 MaterialComponents.DayNight.NoActionBar
(或者如果您的 Activity 没有自定义样式;请在基本应用程序主题上执行此操作)setHomeAsUpIndicator()
won't get executed as theif(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:
NoActionBar
decedent; for instanceTheme.MaterialComponents.DayNight.NoActionBar
(or if your activity doesn't have customized style; do that on the base application theme).if(getSupportActionBar() != null)
condition.