【发布时间】: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