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


当前回答

提示:使用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 );
        }

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

其他回答

在AndroidManifest.xml文件:

<activity
    android:name=".Launch"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <!-- This line is important -->

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>  

或者在Java代码中:

protected void onCreate(Bundle savedInstanceState){
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

如果你不想使用主题@android:style/ theme . notitlebar。因为你已经在使用你自己的主题,你可以使用android:windowFullscreen。

在AndroidManifest.xml:

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

在styles.xml:

<style name="MyTheme"  parent="your parent theme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item> 
</style>

截至2022年

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.decorView.windowInsetsController?.hide(WindowInsets.Type.systemBars())
    } else {
        @Suppress("DEPRECATION") // Older API support
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }

这些答案大部分似乎都过时了。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

对于安卓X

1. 透明状态栏

    window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    window.statusBarColor = Color.TRANSPARENT

2. 透明的状态栏和底部导航栏

    window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
    );

3.隐藏状态栏

API 30+推荐的Compat解决方案(包括向后兼容性)

  val windowInsetsController =
      WindowCompat.getInsetsController(window, window.decorView) ?: return
  windowInsetsController.systemBarsBehavior =
      WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
  windowInsetsController.hide(WindowInsetsCompat.Type.statusBars())

https://developer.android.com/training/system-ui/immersive

4. 隐藏状态栏和底部导航栏

消防级别30:

SystemUiVisibility标志已弃用。请改用WindowInsetsController。

和3一样。,只需使用WindowInsetsCompat.Type.systemBars()

    val actionBar: ActionBar? = supportActionBar
        if (actionBar != null) actionBar.hide()

    val windowInsetsController =
        WindowCompat.getInsetsController(window, window.decorView) ?: return
    windowInsetsController.systemBarsBehavior =
        WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())

https://developer.android.com/training/system-ui/immersive

把这些代码放在哪里?

   override fun onCreate(savedInstanceState: Bundle?) {

        /*  Put above code here ..... */
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_slider)
   }

Note

我在Pixel 3A模拟器中检查了这段代码 可能自定义android操作系统不支持 set style <style name="主题。"全屏Theme.MaterialComponents.DayNight.NoActionBar“父= >