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


当前回答

AndroidManifest.xml

<activity ...
          android:theme="@style/FullScreenTheme"
    >
</activity>

I.你的主应用的主题是theme . appcompat . light . darkactionbar

隐藏ActionBar / StatusBar style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
</style>

<style name="FullScreenTheme" parent="AppTheme">
    <!--this property will help hide the ActionBar-->
    <item name="windowNoTitle">true</item>
    <!--currently, I don't know why we need this property since use windowNoTitle only already help hide actionbar. I use it because it is used inside Theme.AppCompat.Light.NoActionBar (you can check Theme.AppCompat.Light.NoActionBar code). I think there are some missing case that I don't know-->
    <item name="windowActionBar">false</item>
    <!--this property is used for hiding StatusBar-->
    <item name="android:windowFullscreen">true</item>
</style>

隐藏系统导航栏

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        setContentView(R.layout.activity_main)
        ...
    }
 }

2你的主应用主题是theme . appcompat . light . noactionbar

隐藏ActionBar / StatusBar style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
</style>

<style name="FullScreenTheme" parent="AppTheme">
    <!--don't need any config for hide ActionBar because our apptheme is NoActionBar-->
    <!--this property is use for hide StatusBar-->
    <item name="android:windowFullscreen">true</item> // 
</style>

隐藏系统导航栏

类似于theme。appcompat。light。darkactionbar。

演示

其他回答

奇巧有一种叫做沉浸式全屏模式的技术。

例子

你可以通过编程来实现:

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

只需将此代码粘贴到onCreate()方法中

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

从mozilla找到解决方案,他们在这里做了一个扩展库

如果链接中断,这里是代码


/**
 * Retrieves a {@link WindowInsetsControllerCompat} for the top-level window decor view.
 */
fun Window.getWindowInsetsController(): WindowInsetsControllerCompat {
    return WindowInsetsControllerCompat(this, this.decorView)
}


/**
 * Attempts to call immersive mode using the View to hide the status bar and navigation buttons.
 * @param onWindowFocusChangeListener optional callback to ensure immersive mode is stable
 * Note that the callback reference should be kept by the caller and be used for [exitImmersiveModeIfNeeded] call.
 */
fun Activity.enterToImmersiveMode(
    onWindowFocusChangeListener: ViewTreeObserver.OnWindowFocusChangeListener? = null
) {
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    window.getWindowInsetsController().apply {
        hide(WindowInsetsCompat.Type.systemBars())
        systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }

    // We need to make sure system bars do not become permanently visible after interactions with content
    // see https://github.com/mozilla-mobile/fenix/issues/20240
    onWindowFocusChangeListener?.let {
        window.decorView.viewTreeObserver?.addOnWindowFocusChangeListener(it)
    }
}

/**
 * Attempts to come out from immersive mode using the View.
 * @param onWindowFocusChangeListener optional callback to ensure immersive mode is stable
 * Note that the callback reference should be kept by the caller and be the same used for [enterToImmersiveMode] call.
 */
@Suppress("DEPRECATION")
fun Activity.exitImmersiveModeIfNeeded(
    onWindowFocusChangeListener: ViewTreeObserver.OnWindowFocusChangeListener? = null
) {
    if (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON and window.attributes.flags == 0) {
        // We left immersive mode already.
        return
    }
    onWindowFocusChangeListener?.let {
        window.decorView.viewTreeObserver?.removeOnWindowFocusChangeListener(it)
    }
    window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    window.getWindowInsetsController().apply {
        show(WindowInsetsCompat.Type.systemBars())
    }
}

在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);
}