0

我正在看这个教程视频,并写了一个简单的方法来自定义我的 statusBar 和 navigationBar 颜色。这是代码:

  @JvmStatic
  fun hideSystemUiSimplified(appCompatActivity: AppCompatActivity, mainContainer: View,@ColorInt controlBarColor: Int, isLightControlBarAndDarkTextColor: Boolean) {
    // step1: keep the default system ui behavior
    val window = appCompatActivity.window
    WindowCompat.setDecorFitsSystemWindows(window, true)
    // since decorFitsSystemWindow = true, so our fragment stretches from bottomOfStatusBar to topOfNavigationBar
    // so for example, in samsung devices the fragment is not full screen, you see statusBar and navBar,
    // and in google pixel devices, you only see the statusBar but not the navBar.
    // Since we are keeping the default behavior, no need to calculate statusBar and navBar heights etc

    // step2: get windowInsetController: This is the primary thing that controls the controlBars
    val controllerCompat = WindowInsetsControllerCompat(window, mainContainer)
    //step3: simply change the statusBar and navBar color.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      window.statusBarColor = controlBarColor
      window.navigationBarColor = controlBarColor
    }
    controllerCompat.isAppearanceLightStatusBars = isLightControlBarAndDarkTextColor
    controllerCompat.isAppearanceLightNavigationBars = isLightControlBarAndDarkTextColor


    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
      /**------- This is not working. I don't understand what went wrong. Help -----------*/   // <----- Problem here
      val controller = mainContainer.windowInsetsController

      if(isLightControlBarAndDarkTextColor) {
        // enable light statusBar
        // this should change the status bar text color to black
        controller?.setSystemBarsAppearance(
          WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
          WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
        )
      } else {
        // this should change the status bar text color to white
        controller?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
      }
    }
  }

它大部分都在工作。

问题

但在 android 11 上,statusBar 文本颜色没有改变。谁能帮助我或告诉我如何更改 statusBar 文本颜色?

如果您在片段中,以下是该方法的示例用法:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ... ... other codes ... ...
            val solidBlack: Int = ResourcesCompat.getColor(
                requireActivity().resources, R.color.colorSolidBlack, null)
            Accessories.hideSystemUiSimplified(requireActivity() as AppCompatActivity,
            binding.root, solidBlack, isLightControlBar = false
                )
}

请注意,我不想使用已弃用的标志或旧代码。我想学习使用windowInsetsController. 感谢您的阅读。

编辑

我的问题被关闭了。但是调试了1天后,我发现了问题。已设置标志window.decorView.windowSystemUiVisibilityView.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR所以在调用我的方法之前,我只是简单地调用了这段代码:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      val window = appCompatActivity.window
      val windowSystemUiVisibility = window.decorView.windowSystemUiVisibility
      val lightStatusFlag = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

      val afterAnd = windowSystemUiVisibility and lightStatusFlag

      val afterXor = windowSystemUiVisibility xor lightStatusFlag

      Timber.tag(TAG).i("window.decorView.windowSystemUiVisibility = $windowSystemUiVisibility, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR = $lightStatusFlag")
      Timber.tag(TAG).i("andValue = $afterAnd")
      Timber.tag(TAG).i("afterXor = $afterXor")

      if(lightStatusFlag == afterAnd) {
        window.decorView.systemUiVisibility = afterXor
      }
    }
// after that I called the method given before:
hideSystemUiSimplified(... ...)

这为我解决了问题。总之谢谢大家。

4

0 回答 0