4

我遵循了Android 开发教程并获得了全屏粘性沉浸式模式。这使得状态栏和导航栏隐藏起来,直到用户从底部向上或从顶部向下滑动,此时它们会出现并在不使用时慢慢淡出。

问题出在 Android 10(可能还有其他版本)上:屏幕底部的导航栏只是白色图标。没有透明的黑色背景。这使得使用轻量级 UI 很难看清。

有没有办法改变导航栏的背景或图标颜色?特别是出现和消失的粘性沉浸式导航栏(因为它太轻而无法与轻 UI 一起使用)。

我已经尝试过样式并以编程方式设置颜色,但它们只能在非全屏模式下工作:

<!-- does not work -->
<item name="android:navigationBarColor">@android:color/white</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowLightNavigationBar">true</item>

我也试过听,OnSystemUiVisibilityChangeListener所以我可以在导航栏后面显示我自己的透明栏,但它不会触发粘性沉浸模式(如这篇 SO 帖子和视频中所述):

使用粘性沉浸模式检测系统 ui 何时可见

https://www.youtube.com/watch?v=cBi8fjv90E4&feature=youtu.be&t=6m56s

设置全屏的活动代码(kotlin):

protected fun isFullscreen() = defaultSharedPref.getBoolean(SharedPreferenceKey.IS_FULL_SCREEN, false)

private fun supportsFullscreenMode(): Boolean {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (hasFocus && isFullscreen()) toggleFullScreenMode(true)
}

/**
 * "sticky immersive" full screen mode.
 * When user swipes from top/bottom edge, transparent status bar and navigation bar appear and slowly fade out if not used.
 * https://developer.android.com/training/system-ui/immersive
 */
protected fun toggleFullScreenMode(goFullScreen: Boolean) {
    when {
        !supportsFullscreenMode() -> {
            makeActivityToast("Your version of Android is too old to support this feature", Toast.LENGTH_LONG).show()
        }
        goFullScreen -> {
            hideSystemUI()
        }
        !goFullScreen -> {
            showSystemUI()
        }
    }
}

private fun showSystemUI() {
    window.decorView.systemUiVisibility = 0
}

private fun hideSystemUI() {
    window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        // 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)
}
4

0 回答 0