Android 从onPause返回时出现布局显示错误
我有一个布局,其中包括一个GLSURFACHEVIEW,它占据了整个屏幕。在屏幕底部,我有一个半透明的工具栏(LinearLayout),它覆盖在GLSurfaceView上。 另外,我的应用程序是全屏的(Android 从onPause返回时出现布局显示错误,android,glsurfaceview,Android,Glsurfaceview,我有一个布局,其中包括一个GLSURFACHEVIEW,它占据了整个屏幕。在屏幕底部,我有一个半透明的工具栏(LinearLayout),它覆盖在GLSurfaceView上。 另外,我的应用程序是全屏的(android:theme=“@android:style/theme.NoTitleBar.Fullscreen) 通常情况下,一切都很好,但从ICS开始,只要用户返回应用程序(从onPause返回),操作系统播放一个快速动画,应用程序开始时,Android状态栏显示在顶部,应用程序屏幕按下
android:theme=“@android:style/theme.NoTitleBar.Fullscreen
)
通常情况下,一切都很好,但从ICS开始,只要用户返回应用程序(从onPause返回),操作系统播放一个快速动画,应用程序开始时,Android状态栏显示在顶部,应用程序屏幕按下。然后,状态栏消失,应用程序屏幕向上移动这么多,以达到正常的全屏位置
此动画完成后,GLSURFACHEVIEW将覆盖工具栏的上半部分,而工具栏将覆盖GLSURFACHEVIEW的下半部分。看起来底部显示的工具栏部分正好是Android状态栏的大小
当然,我希望工具栏能够完全显示并覆盖GLSurfaceView,这就是从onCreate
启动应用程序时发生的情况
有人知道是什么导致了这个问题以及如何解决它吗
我找到了一个解决办法,但远远不是最佳的。
调用onResume()
后的一段时间,我可以通过将invalidate()
发布到工具栏来正确显示工具栏。但是如果调用invalidate()
太快,问题不会得到解决。
以下是解决方法的代码,在onResume()
启动5秒后调用invalidate()
,使工具栏在应用程序重新启动5秒后正确显示
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
// Test to try to fix half hidden toolbar problem
View toolBar = findViewById(R.id.menu_bar);
toolBar.postDelayed(new Runnable() {
@Override
public void run() {
View toolBar = findViewById(R.id.menu_bar);
toolBar.invalidate();
}
}, 5000);
}
编辑:
上面的代码无法正确显示工具栏。我在上面的代码中的调试TextView
中取出了一行设置文本,如果我不更新完全不相关的TextView
,工具栏就不会显示
我找到了另一种解决上述问题的方法,首先在调用onResume
时删除工具栏,然后稍后将其恢复。请参阅下面的代码:
请注意,使用View.INVISIBLE
而不是View.GONE
将不会使工具栏在延迟后显示
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
// Test to try to fix half hidden toolbar problem
View toolBar = findViewById(R.id.menu_bar);
toolBar.setVisibility(View.GONE);
toolBar.postDelayed(new Runnable() {
@Override
public void run() {
View toolBar = findViewById(R.id.menu_bar);
toolBar.setVisibility(View.VISIBLE);
//mDebugText.setText("Menu bar to front.\n" + mDebugText.getText());
}
}, 5000);
}
最后,我就是这样解决这个问题的。如果有人有更好的解决方案,请务必写在这里,我会选择你的答案作为最佳答案
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Delay for the layout request. It must be longer than the time ICS takes to play
// the animation where the app's screen moves up to fill the space of the Android
// status bar.
final int DELAY_MS = 1000; // in ms
if (hasFocus) {
// Send a delayed layout request to fix the problem with the toolbar
// being half hidden after returning from onPause() on some devices
// with ICS.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
findViewById(R.id.menu_bar).requestLayout();
}
}, DELAY_MS);
}
}
首先,我将代码放在onWindowFocusChanged()
中,因为当关闭屏幕时,onResume(),因此runnable是在应用程序实际显示之前执行的,因此,在动画显示之前,所以在这种情况下问题仍然存在
其次,我将延迟设置为1秒,因为500ms太短。动画没有播放完,工具栏仍然半隐藏。当然,这会导致工具栏显示为半隐藏几毫秒,然后再放回正确的位置。最后,我就是这样解决这个问题的。如果有人有b更好的答案,请务必写在这里,我会选择你的答案作为最佳答案
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Delay for the layout request. It must be longer than the time ICS takes to play
// the animation where the app's screen moves up to fill the space of the Android
// status bar.
final int DELAY_MS = 1000; // in ms
if (hasFocus) {
// Send a delayed layout request to fix the problem with the toolbar
// being half hidden after returning from onPause() on some devices
// with ICS.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
findViewById(R.id.menu_bar).requestLayout();
}
}, DELAY_MS);
}
}
首先,我将代码放在onWindowFocusChanged()
中,因为当关闭屏幕时,onResume(),因此runnable是在应用程序实际显示之前执行的,因此,在动画显示之前,所以在这种情况下问题仍然存在
其次,我将延迟设置为1秒,因为500ms太短。动画没有播放完,工具栏仍然半隐藏。当然,这会导致工具栏显示为半隐藏几毫秒,然后再放回正确的位置。我找到了有关此问题的更多细节和更好的解决方法,因此他re是新的信息
首先,我发现问题只发生在我使用三星的AdHubView
类显示广告时,该类源于RelativeLayout
,因此问题可能与RelativeLayout
有关,也可能与之无关
此广告视图包含在相同的topFrameLayout
中,其中还包括GLSurfaceView
和工具栏(LinearLayout
)。此topFrameLayout
未明确声明,因为我使用了
作为布局的顶部元素
现在,关于更好的解决方法,我所做的是在onPause
中隐藏AdHubView
,然后在onWindowFocusChanged
中再次显示它。我还尝试在onResume
中再次显示它,但在屏幕关闭且我必须解锁屏幕才能返回应用程序的情况下,这不起作用
以下是一些代码示例:
protected void onPause() {
super.onPause();
mAdView.setVisibility(AdHubView.GONE);
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mAdView.setVisibility(AdHubView.VISIBLE);
}
}
有了这个解决方案,我不需要再像以前的解决方案那样等待将工具栏强制到顶部,因此用户不会注意到任何奇怪的事情。我找到了关于这个问题的更多细节和更好的解决方案,下面是新的信息
首先,我发现问题只发生在我使用三星的AdHubView
类显示广告时,该类源于RelativeLayout
,因此问题可能与RelativeLayout
有关,也可能与之无关
此广告视图包含在相同的topFrameLayout
中,其中还包括GLSurfaceView
和工具栏(LinearLayout
)。此topFrameLayout
未明确声明,因为我使用了
作为布局的顶部元素
现在关于更好的解决方法,我所做的是在onPause
中隐藏AdHubView
,然后再次显示它