android设置沉浸式状态栏(kotlin)

本文介绍了如何在Android中使用WindowInsetsControllerCompat解决沉浸式状态栏的问题,包括在BaseActivity中的设置、themes.xml的配置以及LoginActivity的示例,强调了兼容性和自定义标题栏的处理。
摘要由CSDN通过智能技术生成

在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设置沉浸式状态栏就讲到这里了,更多内容,请期待下次的讲述

欢迎有问题的伙伴及时留意讨论,有不足之处还望指正

祝大家生活工作愉快~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值