写到BaseActivity里,所有界面都能调用到 判断版本是否大于21
if(Build.VERSION.SDK_INT > 21){
setStatusBarView()
}
private fun setStatusBarView() {
//延时加载数据,保证Statusbar绘制完成后再findview。
Looper.myQueue().addIdleHandler {
setStatusBarDrawable(this,R.drawable.home_top_gradient_bg)
window.decorView.addOnLayoutChangeListener(
View.OnLayoutChangeListener { view, i, i2, i3, i4, i5, i6, i7, i8 ->
setStatusBarDrawable(this,R.drawable.home_top_gradient_bg)
}
)
false
}
}
private fun setStatusBarDrawable(activity: AppCompatActivity,id:Int){
//利用反射机制修改状态栏背景
val identifier = activity.resources.getIdentifier("statusBarBackground", "id", "android")
val statusBarView: View = activity.window.findViewById(identifier)
if (statusBarView != null) {
statusBarView.setBackgroundResource(id)
}
}
渐变式drawable资源文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="180"
android:endColor="#2c79e2"
android:centerColor="#07C160"
android:startColor="#00FF00"
android:type="linear" />
</shape>