问题描述
有我精的Android Studio,它使用了工具栏 v7.app.ActionBarDrawerToggle 和 NavigationView 而不是导航抽屉活动项目模板在NavigationDrawerFragment(和布局/ fragment_navigation_drawer.xml)。
根据并的,我成立了ActionBarDrawerToggle。我在做它的onCreate 1)实例化,2)调用syncState在onPostCreate 3)通过调用onConfigurationChanged和onOptionsItemSelected。
这几乎是完美的工作除了一件事:汉堡图标显示为一个箭头
有一个类似的问题可以在计算器上找到,尤其是对this问题。但问题是关于使用旧R.drawable.ic_drawer汉堡包而不是材质设计(前棒棒堂5.0)版本的方式。此外,该问题没有答案,提问者评论他没有说明任何解决方案已经解决了。
过了一会儿,我已经找到了解决办法意外。这是有点脏。这是调用syncState中的onCreate。因为它似乎是,由于某种原因,onPostCreate未在我的应用程序调用。实际上,这个肮脏的解决方案中回答其他问题。
但来的调用syncState在onPostCreate 的。为什么不工作?为什么我的应用程序调用onPostCreate?这应该是不被显示汉堡包图标(而不是显示箭头)的主要原因。
下面是我的code:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){ ... 工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏);
动作条动作条= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(真); drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
drawerToggle =新ActionBarDrawerToggle(
这个,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
){
@覆盖
公共无效onDrawerClosed(查看drawerView){
super.onDrawerClosed(drawerView); invalidateOptionsMenu();在prepareOptionsMenu //调用()
} @覆盖
公共无效onDrawerOpened(查看drawerView){
super.onDrawerOpened(drawerView); invalidateOptionsMenu();在prepareOptionsMenu //调用()
}
};
drawerLayout.setDrawerListener(drawerToggle); navigationView =(NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(本); drawerToggle.syncState(); //调用这个位置是有点脏的解决方案
}@覆盖
公共无效onPostCreate(捆绑savedInstanceState,
PersistableBundle persistentState){
super.onPostCreate(savedInstanceState,persistentState);
drawerToggle.syncState();
}@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
drawerToggle.onConfigurationChanged(NEWCONFIG);
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
如果(drawerToggle.onOptionsItemSelected(项目)){
返回true;
} ...}
下面onPostCreate:
@覆盖
公共无效onPostCreate(捆绑savedInstanceState,
PersistableBundle persistentState){
super.onPostCreate(savedInstanceState,persistentState);
drawerToggle.syncState();
}
应该是这样的:
@覆盖
保护无效onPostCreate(捆绑savedInstanceState){
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
有两种类型onPostCreate的
- 有两个参数。
- 带一个参数。
您应该已经犯了一个错误选择前者在覆盖在Android工作室的方法。
I have refined the Navigation Drawer Activity project template of Android Studio, which uses Toolbar, v7.app.ActionBarDrawerToggle and NavigationView instead of the NavigationDrawerFragment (and layout/fragment_navigation_drawer.xml).
According to Google's guidance and reference, I set up ActionBarDrawerToggle. I made it 1) instantiate in onCreate, 2) call syncState in onPostCreate and 3) call through to onConfigurationChanged and onOptionsItemSelected.
It is almost perfectly working except for one thing: the hamburger icon is showed as an arrow.
A similar issue can be found on the StackOverFlow, especially for this question. But the question is about the way using the old R.drawable.ic_drawer as hamburger which is not the material design (before 5.0 Lollipop) version. Moreover the question has no answer and the questioner commented he had solved without stating any solution.
After a while, I have found a solution accidentally. It is somewhat dirty. It is to call syncState in onCreate. Because it seems that, for some reason, onPostCreate is not called in my app. Actually, this dirty solution is used in an answer to the other problem.
But the official reference says to call syncState in onPostCreate. Why doesn't it work? Why doesn't my app call onPostCreate? This should be main cause of not being showed hamburger icon (instead being showed arrow).
Below is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle.syncState(); // calling this here is somewhat a dirty solution
}
@Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
}
Here onPostCreate:
@Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
It should be this:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
There are two types of onPostCreate:
- Activity's onPostCreate with two arguments.
- AppCompatActivity's onPostCreate with a single argument.
You should have made a mistake to choose the former one when you override a method on Android Studio.
这篇关于箭头显示,而不是材料设计版本汉堡包图标。为什么不onPostCreate工作syncState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!