How to change the status bar color in Android?

后端 未结 21 2021
旧时难觅i
旧时难觅i 2020-11-21 16:09

First of all it\'s not a duplicate as in How to change the background color of android status bar

How do I change the status bar color which should be same as in nav

21条回答
  •  长发绾君心
    2020-11-21 16:34

    If you want set custom drawable file use this code snippet

    fun setCustomStatusBar(){
        if (Build.VERSION.SDK_INT >= 21) {
            val decor = window.decorView
            decor.viewTreeObserver.addOnPreDrawListener(object :
                ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    decor.viewTreeObserver.removeOnPreDrawListener(this)
                    val statusBar = decor.findViewById 
                      (android.R.id.statusBarBackground)
                    statusBar.setBackgroundResource(R.drawable.bg_statusbar)
                    return true
                }
            })
        }
    }
    

提交回复
热议问题