android 状态栏和虚拟按键栏字体背景颜色设置

铁石心肠 1年前 ⋅ 1237 阅读

1、设置顶部状态栏字体颜色为白色红底

  1.  
    public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
  2.  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
  3.  
    View decorView = context.getWindow().getDecorView();
  4.  
    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  5.  
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  6.  
    | View.SYSTEM_UI_FLAG_VISIBLE;
  7.  
    decorView.setSystemUiVisibility(option);
  8.  
     
  9.  
    //根据上面设置是否对状态栏单独设置颜色
  10.  
    if (useThemeStatusBarColor) {
  11.  
    context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
  12.  
    } else {
  13.  
    context.getWindow().setStatusBarColor(Color.TRANSPARENT);
  14.  
    }
  15.  
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
  16.  
    WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
  17.  
    localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
  18.  
    }
  19.  
    }

2、设置状态栏字体颜色为黑色

  1.  
    public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
  2.  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  3.  
    View decorView = context.getWindow().getDecorView();
  4.  
     
  5.  
    context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
  6.  
     
  7.  
    if (useThemeStatusBarColor) {
  8.  
    context.getWindow().setStatusBarColor(Color.parseColor("#FF3E96"));
  9.  
    context.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));//40000000
  10.  
    } else {
  11.  
    context.getWindow().setStatusBarColor(Color.TRANSPARENT);
  12.  
    }
  13.  
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
  14.  
    WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
  15.  
    localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
  16.  
    }
  17.  
    }

3、设置虚拟按键栏背景颜色

  1.  
    public static void setNavigationBarColor(Activity activity){
  2.  
     
  3.  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  4.  
    activity.getWindow().setNavigationBarColor(Color.parseColor("#FF3E96"));
  5.  
    }
  6.  
     
  7.  
    }

4、设置虚拟按键栏和状态栏字体图标为黑色,白底

  1.  
    public static void setStatusBarColor(Activity context, boolean useThemeStatusBarColor) {
  2.  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
  3.  
    context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
  4.  
     
  5.  
    //根据上面设置是否对状态栏单独设置颜色
  6.  
    if (useThemeStatusBarColor) {
  7.  
    context.getWindow().setStatusBarColor(Color.parseColor("#ffffffff"));
  8.  
    context.getWindow().setNavigationBarColor(Color.parseColor("#ffffffff"));//40000000
  9.  
    } else {
  10.  
    context.getWindow().setStatusBarColor(Color.TRANSPARENT);
  11.  
    }
  12.  
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
  13.  
    WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();
  14.  
    localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
  15.  
    }
  16.  
    }

这些方法不一定适配所有手机,如果是DialogFragment就将activity改成Dialog即可。

转载:https://blog.csdn.net/u011324501/article/details/92722921?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-8-92722921.nonecase&utm_term=android%20%E8%AE%BE%E7%BD%AE%E8%99%9A%E6%8B%9F%E9%94%AE%E7%99%BD%E8%89%B2


全部评论: 0

    我有话说:

    咨询问题

    点击这里给我发消息

    淘宝咨询

    博主承接代做业务

    真·送云服务器

    咨询博主