正如正确推断的那样,您得到的答案是由于状态栏。您所要做的就是在窗口显示启动之前摆脱状态栏。然后您可以在设置活动的内容视图之前重置状态栏。
这样做的原因是摆脱状态栏会影响您的视图绘图,除非您动态处理所有测量、布局和绘图。这样做是在运行时的中间,会导致状态栏消失,然后根据需要重新出现,从而导致用户混淆。
隐藏状态栏:
在你的 onCreate() 中:
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Add the flag to the Window Attributes
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
//Disassociate Display from the Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
现在您的默认显示应该可以正常工作了
仍然在你的 onCreate() 中:
Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
现在在设置内容之前
再次,在您的 onCreate() 中:
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Show the statubar
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
// Reassociate.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
最后:
setContentView(r.layout.myView);
实际上,前面的代码段几乎可以在任何地方工作。我只是在考虑您的用户体验。当然,随意将它们放置在任何地方。这些是从我的一个项目中提取的功能部分。我在一些 Home Launcher 中也看到了类似的技术。注意:根据 Android 版本,您可能需要在 onWindowAttached() 中处理状态栏。如果这样做,请确保您仍然调用 super.onWindowAttached()。
另一种技巧:
当然,如果您仍然想这样做,您可以随时在清单中以这种方式设置活动的属性。
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"