Android-StatusBarUtil沉浸式状态栏


一、前言

在这里插入图片描述

大家再开发过程中会遇到上面这种情况,它不影响使用但是在美观上面差点意思,接下来教给大家一种方式来使它美观

二、去掉标题栏

只需要改变NoActionBar

在这里插入图片描述

三、StatusBarUtil属性

  • 设置状态栏颜色
StatusBarUtil.setColor(Activity activity, int color)
  • 设置状态栏半透明
StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha)
//通过传入 statusBarAlpha 参数,可以改变状态栏的透明度值,默认值是112。
  • 设置状态栏全透明
StatusBarUtil.setTransparent(Activity activity)
  • 为使用 ImageView 作为头部的界面设置状态栏透明
StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView)

四、沉浸状态栏颜色

是不是已经有了明显改善,但是上面还有一块颜色格格不入

沉浸式状态栏,既可以把颜色实现沉浸,又可以把图片实现沉浸。

引入依赖

implementation 'com.jaeger.statusbarutil:library:1.5.1'

未设置颜色沉浸的状态:
在这里插入图片描述
核心代码:

StatusBarUtil.setColor(MainActivity.this,getResources().getColor(R.color.bisque),0);

设置完成后的状态栏:
在这里插入图片描述

五、沉浸状态栏图片

未沉浸之前的效果:
在这里插入图片描述
设置完成后的状态栏:
在这里插入图片描述

核心代码:

StatusBarUtil.setTranslucentForImageView(this, 0, constraintLayout);

MainActivity代码

public class MainActivity extends AppCompatActivity {
    ConstraintLayout constraintLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        constraintLayout=findViewById(R.id.constraintlayout);
        StatusBarUtil.setTranslucentForImageView(this, 0, constraintLayout);

    }
}

XML代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/fengjing" />
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

详细方法参考官方文档
StatusBarUtil官网

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值