在android设置沉浸式状态栏时,由于android碎片化的原因,一直以来是个难题。随着android官方的迭代,可以使用WindowInsetsControllerCompat进行方便的设置。
一、编写基类BaseActivity.kt
abstract class BaseActivity: AppCompatActivity() {
lateinit var insertControllerCompat :WindowInsetsControllerCompat;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//compat版本,向下兼容
insertControllerCompat = WindowCompat.getInsetsController(window, window.decorView)!!
//insertControllerCompat?.show(WindowInsetsCompat.Type.systemBars()) // 展示系统view:状态栏+导航栏+系统标题栏。在使用getInsetsController后,
//需先在xml styles隐藏actionBar(系统标题栏),不要用代码(如getSupportActionBar()?.hide()等)设置该系统标题栏隐藏,且insetsController不能单独隐藏系统标题栏
//insertControllerCompat?.hide(WindowInsetsCompat.Type.navigationBars()) //隐藏底部手势导航栏
//insertControllerCompat?.hide(WindowInsetsCompat.Type.statusBars()) //隐藏状态栏
WindowCompat.setDecorFitsSystemWindows(window, false) // 页面布局是否在状态栏下方,false:侵入状态栏
insertControllerCompat?.isAppearanceLightStatusBars = true //状态栏字体明暗主题,true为黑色(默认黑色),false为白色
window.statusBarColor = Color.TRANSPARENT //状态栏背景色,侵入时最好设置为透明
}
//在继承BaseActivity的类渲染完(setContentView)页面后,再调用该方法,将我们的自定义标题栏下移,且可以设置状态栏文字颜色
fun setPaddingTopAndTitleIsWhite(isWhite : Boolean){
val customBar = findViewById<LinearLayout>(R.id.xbar_title) //自己制作标题栏
ViewCompat.setOnApplyWindowInsetsListener(customBar) { view, insets ->
val params = view.layoutParams as ConstraintLayout.LayoutParams
params.topMargin = insets.getInsets(WindowInsetsCompat.Type.statusBars()).top //将第一个控件下移状态栏的距离
Log.d("状态栏下移","..." + insets.getInsets(WindowInsetsCompat.Type.statusBars()).top)
insets
}
//设置字体为白色
if(isWhite){
insertControllerCompat?.isAppearanceLightStatusBars = false
}
}
fun turnToActivity(clz: Class<out Activity>) {
val intent = Intent(this, clz)
startActivity(intent)
}
}
二、编辑themes.xml,关闭系统标题栏
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
三、编写LoginActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
setPaddingTopAndTitleIsWhite(true); //设置沉浸式状态栏和文字颜色
}
四、activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#019BDB"
>
<LinearLayout
android:id="@+id/xbar_title"
android:background="#ff1111"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="返回"
android:background="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
...
</androidx.constraintlayout.widget.ConstraintLayout>
五、效果:
好了,本次关于android设置沉浸式状态栏就讲到这里了,更多内容,请期待下次的讲述
欢迎有问题的伙伴及时留意讨论,有不足之处还望指正
祝大家生活工作愉快~