Android Material Design 沉浸式状态栏

Android 沉浸式状态栏

Android 4.4 版本之前,我们是无法改变手机的状态栏的颜色。也就是说,无论打开哪个应用,状态栏都是黑色,与应用的风格非常不协调。为了提供更好的界面交互,google在Android 4.4 之后提供了设置沉浸式状态栏的方法。实际上的效果就是给状态栏设置透明的背景。

1 隐藏状态栏
1.1 隐藏状态栏和ActionBar
  public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }}

这里先调用getWindow().getDecorView()的方法获取当前界面的DecorView,然后调用它的seteSystemUiVisibility()方法来设置系统UI元素的可见性。SYSTEM_UI_FLAG_FULLSCREEN 代表全屏的意思,也就是将状态栏隐藏。另外,根据Android设计建议,ActionBar是不应该独立于状态栏而单独显示,因此状态栏如果隐藏了,我们同时也需要调用ActionBar的hide()方法将ActionBar()也进行隐藏。

1.2 Android 4.4 - 5.0 ,Android 5.0 以上,隐藏状态栏

1.隐藏状态栏
在状态中添加

    true
  1. 获取状态栏高度

       private static int getStatusBarHeight(Activity activity){
           //获取状态栏的高度
           Resources resources = activity.getResources();
           int statusBarHeight = resources.getIdentifier("status_bar_height", "dimen", "android");
           return resources.getDimensionPixelOffset(statusBarHeight); 
      }
    
  2. 设置全屏

          public static void setActivityTranslucent(Activity activity){
          //5.0以上
          if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
           //获取DecorView
            View decorView = activity.getWindow().getDecorView();
           decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
           activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
          }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){//4.4 - 5.0 之间
           activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
       }
     }
    

4.沉浸式状态栏

    public static void setStatusBarTranslucent(Activity activity){
    //5.0以上
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        //获取DecorView
        View decorView = activity.getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
    }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){//4.4 - 5.0 之间
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //在状态栏的部分添加一个布局 setContentView 的源码,自己添加一个布局(高度是状态栏的高度)
        View view = new View(activity);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity));
        view.setLayoutParams(params);
        view.setBackgroundColor(Color.TRANSPARENT);

        ViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView();
        decorView.addView(view);

        //获取activity中setContentView布局的根布局
        ViewGroup contentView = activity.findViewById(android.R.id.content);
        contentView.setPadding(0,getStatusBarHeight(activity),0,0);
    }
}

你可能感兴趣的:(Android Material Design 沉浸式状态栏)