我如何使一个活动全屏?没有通知栏。


当前回答

getWindow () .setFlags (WindowManager.LayoutParams。FLAG_LAYOUT_NO_LIMITS WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);if (getSupportActionBar() != null){getSupportActionBar().hide();}

其他回答

这些答案大部分似乎都过时了。Developer.android.com建议这样做:

科特林:

private fun hideSystemBars() {
    val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) ?: return
    windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}

Java:

private void hideSystemBars() {
    WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
    if (windowInsetsController == null) {
        return;
    }
    windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}

更多信息:https://developer.android.com/training/system-ui/immersive

 protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_splash_screen);
    getSupportActionBar().hide();

}

提示:使用getWindow().setLayout()会搞砸你的全屏显示!注意,这个方法的文档说:

设置窗口的宽度和高度布局参数… 你可以把它们改成……用于制作非全屏窗口的绝对值。

http://developer.android.com/reference/android/view/Window.html#setLayout%28int,%20int%29

For my purposes, I found that I had to use setLayout with absolute parameters to resize my full screen window correctly. Most of the time, this worked fine. It was called by an onConfigurationChanged() event. There was a hiccup, however. If the user exited the app, changed the orientation, and reentered, it would lead to firing off my code which included setLayout(). During this re-entry time window, my status bar (which was hidden by the manifest) would be made to re-appear, but at any other time setLayout() would not cause this! The solution was to add an additional setLayout() call after the one with the hard values like so:

       public static void setSize( final int width, final int height ){
//DO SOME OTHER STUFF...
            instance_.getWindow().setLayout( width, height );
            // Prevent status bar re-appearance
            Handler delay = new Handler();
            delay.postDelayed( new Runnable(){ public void run() {
                instance_.getWindow().setLayout(
                    WindowManager.LayoutParams.FILL_PARENT,
                    WindowManager.LayoutParams.FILL_PARENT );
            }}, FILL_PARENT_ON_RESIZE_DELAY_MILLIS );
        }

然后,窗口正确地调整大小,状态栏不会重新出现,不管触发这一事件。

我想使用我自己的主题,而不是使用@android:style/ theme . notitlebar . fullscreen。但它并没有像这里的一些帖子所提到的那样工作,所以我做了一些调整来解决它。

在AndroidManifest.xml:

<activity
    android:name=".ui.activity.MyActivity"
    android:theme="@style/MyTheme">
</activity>

在styles.xml:

<style name="MyTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
</style>

注意:在我的情况下,我必须使用name="windowActionBar"而不是name="android:windowActionBar"在它正常工作之前。所以我只是使用了这两种方法,以确保我以后需要移植到新的Android版本。

谢谢你的回答@克里斯蒂安,我得到了错误

android.util.AndroidRuntimeException:必须调用requestFeature() 添加内容前

我用

@Override
protected void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_login);

    -----
    -----
}