Android 中隐藏标题栏和状态栏的方法

在Android开发中,隐藏标题栏和状态栏是实现全屏显示的常见需求。

一、隐藏标题栏

1、通过代码隐藏

对于继承自 AppCompatActivity 的 Activty,可在 onCreate() 方法中调用supportRequestWindowFeature 或 getSupportActionBar 方法来隐藏标题栏。

复制代码
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 隐藏标题栏方法1,必须在 setContentView 方法之前调用
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)

        setContentView(R.layout.activity_second)

        // 隐藏标题栏方法2, 调用位置没有限制
        supportActionBar?.hide()
    }

对于普通 Activity,采用 requestWindowFeature 方法。

复制代码
requestWindowFeature(Window.FEATURE_NO_TITLE)

2、通过主题隐藏

AndroidManifest.xml 文件中,为 <application> 标签或特定 <activity> 标签设置主题。

复制代码
	<application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TwoApkSwitch"
        tools:targetApi="31">
   
   		......
        <activity android:name=".SecondActivity" 
        	android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"/>

二、隐藏状态栏

1、通过代码隐藏

如果隐藏状态栏后,用户下拉屏幕可能会重新显示状态栏,可以通过监听onSystemUiVisibilityChange() 方法来重新隐藏。

复制代码
	/**
     * 隐藏状态栏
     */
    fun hideStateBar() {
        Log.d("SecondActivity", "version: ${Build.VERSION.SDK_INT}")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // Android 11及以上版本
            val controller = window.insetsController
            if (controller != null) {
                controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
                controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        } else {
            // Android 4.1及以上版本
            val decorView: View = window.decorView
            val uiOptions: Int = View.SYSTEM_UI_FLAG_FULLSCREEN
            decorView.setSystemUiVisibility(uiOptions)
			// 用户下拉屏幕可能会重新显示状态栏,可以通过监听后重新隐藏
            decorView.setOnSystemUiVisibilityChangeListener {
                // 重新隐藏
            }
  
            // Android 4.0及以下版本
            window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
        }
    }

2、通过主题隐藏

AndroidManifest.xml 文件中,为 <application> 标签或特定 <activity> 标签设置主题。

可自定义主题,并在主题中添加 windowFullscreen 字段为 true。

复制代码
	<activity android:name=".SecondActivity"
            android:theme="@style/Theme.TwoApkSwitch.FullScreen"/>

在 themes.xml 中自定义隐藏状态栏主题。

复制代码
    <style name="Theme.TwoApkSwitch.FullScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->

		<!--  隐藏上方时间内容状态栏-->
        <item name="android:windowFullscreen">true</item>
    </style>
相关推荐
夜雪一千21 小时前
MySQL 全局锁是什么?原理、风险、备份踩坑完整实战
android·mysql·adb
又见情义1 天前
RK3568 Android 13 驱动适配实战:从零开始让SDK在自己的板子上跑起来
android·arm开发·驱动开发
霸道流氓气质1 天前
MySQL 大表 DDL 变更 — 原理、方案与实践
android·数据库·mysql
明雨-开发1 天前
Android打包时候Execution failed for task ‘:launcher:lintVitalAnalyzeRelease‘.
android
游戏开发爱好者81 天前
开心上架是什么,一站式 Apple 开发者工作台总览
android·小程序·https·uni-app·iphone·webview
Android-Flutter1 天前
android kotlin launch 源码分析
android·kotlin
Android-Flutter1 天前
android kotlin 状态机 Continuation 详解
android·kotlin
Zha0Zhun1 天前
Jetpack Compose 实现 iOS 风格 3D WheelPicker
android
深念Y1 天前
RIO-UL00(EMUI 4.1 / Android 6.0.1 / arm64)开机自启动 sshd
android·linux·华为·安卓·chroot·sshd·emui
爱和冰阔落1 天前
【MySQL 慢查询排查实战】列表接口逐渐变慢时,怎样从请求链路定位原因
android·数据库·mysql