我目前正在我的应用中实施导航抽屉。事情进展顺利,但是我已经遇到了一个问题,抽屉认为在旋转设备后它已经关闭,尽管它仍然可见。以下是一些说明问题的屏幕截图:
轮换前:
轮换后:
历史记录是我在打开抽屉之前查看的部分的标题。我在onSaveInstanceState()
中保留了ActionBar标题并在onCreate()
中恢复,但我用来设置标题的功能应该在抽屉打开时将标题设置为应用名称:
private void setTitle(String title)
{
gSectionTitle = title;
TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
if(actionBarTitle != null)
{
// Only set a title if we're looking at History or if the Navigation Drawer is open
Boolean navDrawerOpen = gNavDrawerLayout.isDrawerOpen(gNavDrawerList);
// DEBUG
Log.i(TAG, "navDrawerOpen: " + navDrawerOpen + ", section: " + gSection + ", section title: " + gSectionTitle);
if(navDrawerOpen)
{
actionBarTitle.setText(gNavDrawerTitle);
}
else if(gSection == SECTION_HISTORY)
{
actionBarTitle.setText(gSectionTitle);
}
else
{
actionBarTitle.setText("");
}
}
}
(我使用自定义ActionBar布局,因此我无法使用getActionBar().setTitle()
。)
您会注意到我在该功能中有Log.i()
- 这是导航抽屉打开时的内容(如第一张图片所示):
navDrawerOpen: true, section: 1, section title: gSho
这是旋转后调用时的内容(如第二张图片所示):
navDrawerOpen: false, section: 1, section title: History
正如您所看到的,导航抽屉保持打开状态,但应用程序认为它已关闭,因此它恢复了当前可见部分的标题。我按照导航抽屉教程,我记得要设置onPostCreate()
和onConfigurationChanged()
:
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
gNavDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
gNavDrawerToggle.onConfigurationChanged(newConfig);
}
这里没什么特别的,但这里的ActionBarDrawerToggle
完全相同:
// Change the ActionBar whenever the drawer is opened or closed
gNavDrawerToggle = new ActionBarDrawerToggle(this, gNavDrawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name)
{
@Override
public void onDrawerOpened(View drawerView)
{
// As per navigation drawer guidelines, show the app name and hide elements on the ActionBar
setTitle(gNavDrawerTitle);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView)
{
setTitle(gSectionTitle);
// Show hidden Action Bar elements
invalidateOptionsMenu();
}
};
以上代码正常直到我旋转我的设备,此时错误的标题将恢复到ActionBar,因为它认为导航抽屉在它仍然打开时关闭。我在这里做错了什么?
答案 0 :(得分:0)
尝试添加对super.onDrawerClosed()
和super.onDrawerOpened()
的调用,如下所示:
// Change the ActionBar whenever the drawer is opened or closed
gNavDrawerToggle = new ActionBarDrawerToggle(this, gNavDrawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name)
{
@Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
// As per navigation drawer guidelines, show the app name and hide elements on the ActionBar
setTitle(gNavDrawerTitle);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
setTitle(gSectionTitle);
// Show hidden Action Bar elements
invalidateOptionsMenu();
}
};
答案 1 :(得分:0)
您在DrawerLayout
有足够的时间进行布局之前调用它。您可以在View.post
上致电DrawerLayout
,然后将其记录在Runnable
。
答案 2 :(得分:-1)
尝试将此添加到您的活动AndroidManifest.xml中。这可以避免在旋转设备时重新启动活动。此外,当您隐藏键盘和屏幕尺寸更改时。
机器人:configChanges = “keyboardHidden |取向|屏幕尺寸”