另外两种Android沉浸式状态栏实现思路

2019-12-10 19:23:06王旭

这样出来的效果就会很丑,于是才有了改进版的方案,不知QQ是否是这样做的。
除了上述的缺陷以外,还有一点看着不是很舒服,就是当我使用抽屉菜单或者滑动返回效果的时候是这样的

另外两种Android沉浸式状态栏实现思路

状态栏并没有阴影效果

我想要的效果是这样的

另外两种Android沉浸式状态栏实现思路

状态栏也会跟着一起滑动

第一种思路
自定义一个状态栏,不能添加“ android:fitsSystemWindows="true"
”这个属性,不然无法填充到状态栏,如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical">

 <View
  android:id="@+id/status_bar"
  android:layout_width="match_parent"
  android:layout_height="20dp"/>

 <FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

</LinearLayout>

在到代码中判断

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  View statusBar = findViewById(R.id.status_bar);

  setContentView(R.layout.activity_test);
  //判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,不然低版本会多出来一个
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   setTranslucentStatus(true);
   statusBar.setVisibility(View.VISIBLE);
   //还有设置View的高度,因为每个型号的手机状态栏高度都不相同
  }else{
   statusBar.setVisibility(View.GONE);
  }
 }
 @TargetApi(19)
 private void setTranslucentStatus(boolean on) {
  Window win = getWindow();
  WindowManager.LayoutParams winParams = win.getAttributes();
  final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
  if (on) {
   winParams.flags |= bits;
  } else {
   winParams.flags &= ~bits;
  }
  win.setAttributes(winParams);
 }