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


当前回答

截至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)
    }

其他回答

通过缺口或切口区域显示内容。这可以从文档中得到帮助:

LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES -在纵向和横向模式下将内容呈现到剪切区域。

对我来说最重要的是activity样式中的这一行:

// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 

对我来说,我想以沉浸式模式展示图像。当我点击它时,我希望系统UI(状态和导航栏)显示出来。

以下是我的解决方案:

在Activity中,一些显示/隐藏系统UI的方法(状态条/导航条)

private fun hideSystemUI() {
    sysUIHidden = true
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
            or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
            )
}


private fun showSystemUI() {
    sysUIHidden = false
    window.decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
            )
}

2-确保在你的xml布局的根视图中

android:fitsSystemWindows="false"

3-全屏活动的风格将给状态/导航栏显示时一个半透明的背景:

<style name="FullscreenTheme" parent="AppTheme">
    <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowBackground">@null</item>
    <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
    <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    <item name="android:statusBarColor">#50000000</item>
    <item name="android:navigationBarColor">#50000000</item>
    // Important to draw behind cutouts
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> 
</style>

<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/sysTransparent</item>
</style>

科特林

遵循谷歌文档,有一个简单的方法:

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideSystemUI() }


private fun hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
        // Set the content to appear under the system bars so that the
        // content doesn't resize when the system bars hide and show.
        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        // Hide the nav bar and status bar
        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_FULLSCREEN)      }


// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private fun showSystemUI() {
window.decorView.systemUiVisibility = 
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)       }

谷歌文档

你可以通过编程来实现:

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

或者你也可以通过AndroidManifest.xml文件来实现:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

编辑:

如果你正在使用AppCompatActivity,那么你需要添加新的主题

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

然后使用它。

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>

感谢https://stackoverflow.com/a/25365193/1646479

如果你不想使用主题@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>

对于安卓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“父= >