文章目录
- 一、前言
- 二、去掉标题栏
- 三、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;@Overrideprotected 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"><ImageViewandroid: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.ConstraintLayoutandroid:id="@+id/constraintlayout"android:layout_width="match_parent"android:layout_height="match_parent"></androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
详细方法参考官方文档
StatusBarUtil官网