Android沉浸式模式状态栏

参考链接:https://blog.csdn.net/guolin_blog/article/details/51763825
https://www.jianshu.com/p/2ef52f357aa0
之前一直想记录一篇在Android上实现沉浸式模式状态栏的文章,本人太懒一直没写,今天终于才记录一下。学习,总结还是要趁早,别懒~
本文只记录Android5.0以上实现的效果,如果要看Android4.4的实现效果可移步这篇文章:https://blog.csdn.net/lmj623565791/article/details/48649563

上次写的透明状态栏的文章没有涉及到兼容Android4.4到Android5.0之间,这里来补充下
  • 直接贴上代码,风章程了一个工具类,可以拿过去直接用
/**
 * Created By pq
 * on 2019/1/30
 * 状态栏适配
 */
public class StatusBarUtil {

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarH(Context context) {
        int height = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            height = context.getResources().getDimensionPixelSize(resourceId);
        }
        return height;
    }


    public static View createStatusBarView(Context context, @ColorInt int color){
        View stausView=new View(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.
                LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                getStatusBarH(context));
        stausView.setLayoutParams(layoutParams);
        stausView.setBackgroundColor(color);
        return stausView;
    }

    public static void setStatuBar(Activity activity,@ColorInt int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0+
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //取消透明状态栏,contentView不再与状态栏重叠
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().setStatusBarColor(color);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4
            //透明状态栏,contentView与状态栏重叠,需使用fitSystemWindow属性
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(createStatusBarView(activity, color));
        }
    }

}
以下是之前的Android5.0+的状态栏的适配

下面看初始的布局文件(布局文件可随便写)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".activitys.StatuBarActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/xv_x"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我的主题风格设置的是:

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

当前的activity的代码:

public class StatuBarActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statu_bar);

    }
}
我们运行看下效果:

《Android沉浸式模式状态栏》 image.png

接下来我们在代码中来实现我们想要的沉浸式模式的状态栏的效果,在activity的setContentView后添加如下代码:

if (Build.VERSION.SDK_INT>21){
            View decorView = getWindow().getDecorView();
            int options=View.SYSTEM_UI_FLAG_FULLSCREEN
                    |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    |View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(options);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
        }

背景图由于是白色的状态栏效果不明显,背景图换了张带底色的,如下图:

《Android沉浸式模式状态栏》

如果是添加的toolBar,要将toolBar跟状态栏荣融为一体,运行之后效果如下图:

《Android沉浸式模式状态栏》 image.png

这里看到的可能不是很明显,其实我手机上看到的是ToolBar被挤到了状态栏,占用了状态栏的空间,怎么办呢?我们只要在跟布局的子布局:ToolBar的布局中加入一下这句代码,即可:

android:fitsSystemWindows="true"

运行效果如下:

《Android沉浸式模式状态栏》 image.png

到此,效果呈现,下回再见 ~……
    原文作者:码后泡
    原文地址: https://www.jianshu.com/p/cd4f2c2987ce
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞