沉浸式状态栏

  • 状态栏设置为纯色

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(Color.WHITE);
    }

  • build.gradle dependencies中加入以下依赖

compile 'com.android.support:appcompat-v7:25.3.1'

设置主题 values-v23 (状态栏字体颜色会根据状态栏的颜色而改变,深色状态栏为白色字体,浅色状态栏为黑色字体)


  • 状态栏为图片的一部分

     protected void setImmerseLayout(View view) {// view为标题栏
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
             Window window = getWindow();
             window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
             int statusBarHeight = getStatusBarHeight(this.getBaseContext());
             int padding = DensityUtil.dip2px(this, 20);
             view.setPadding(padding, padding + statusBarHeight, padding, padding);
         }
     }
    
     public int getStatusBarHeight(Context context) {
         int result = 0;
         int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                 "android");
         if (resourceId > 0) {
             result = context.getResources().getDimensionPixelSize(resourceId);
         }
         return result;
     }
    

你可能感兴趣的:(android)