这里如果将ColorPrimary和colorPrimaryDark颜色值设置成一致,那么将会成为“沉浸式”,或者说是透明状态栏

 

注意:他们android不同版本上实现方法不同

image.png
 /** * 设置状态栏颜色 * * @param activity       需要设置的activity * @param color          状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */public static void setColor(Activity activity, @ColorInt int color, int statusBarAlpha) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha));    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      //版本是大于19,设置背景为透明        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();        int count = decorView.getChildCount();        if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {            decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));        } else {            StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha);            decorView.addView(statusView);        }        setRootView(activity);    }} /** * 计算状态栏颜色 * * @param color color值 * @param alpha alpha值 * @return 最终的状态栏颜色 */private static int calculateStatusColor(@ColorInt int color, int alpha) {    float a = 1 - alpha / 255f;    int red = color >> 16 & 0xff;    int green = color >> 8 & 0xff;    int blue = color & 0xff;    red = (int) (red * a + 0.5);    green = (int) (green * a + 0.5);    blue = (int) (blue * a + 0.5);    return 0xff << 24 | red << 16 | green << 8 | blue;}

更多相关文章

  1. Android(安卓)shape 矩形边框
  2. android studio3.1.4更新3.2.1版本问题汇总
  3. Android(安卓)9.0 (P版本) 原生的电池-Google省电模式
  4. Android生成apk后防反编译
  5. Android(安卓)HelloWorld
  6. Android各版本系统源代码下载
  7. Android(安卓)MaterialButton的一些问题
  8. EditText 修改光标颜色及位置
  9. Android使用ListView应该注意的地方

随机推荐

  1. 问题1.Activity为什么开始要调用onCreate
  2. Android界面布局基本属性
  3. 使用ListActivity得到ListView
  4. ListView的使用2
  5. 沃邮箱Android客户端产品体验报告
  6. Android NDK移植libiconv和libxml2
  7. Eclipse 环境下安装PhoneGap开发插件
  8. Android - bitmap简单总结
  9. android 开源 OCR 项目 及手写识别[转]
  10. android 的文本框输入类型