How to change the status bar color in Android?

后端 未结 21 2259
旧时难觅i
旧时难觅i 2020-11-21 16:09

First of all it\'s not a duplicate as in How to change the background color of android status bar

How do I change the status bar color which should be same as in nav

21条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 16:31

    This is what worked for me in KitKat and with good results.

    public static void setTaskBarColored(Activity context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            {
                Window w = context.getWindow();
                w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //status bar height
                int statusBarHeight = Utilities.getStatusBarHeight(context);
    
                View view = new View(context);
                view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                view.getLayoutParams().height = statusBarHeight;
                ((ViewGroup) w.getDecorView()).addView(view);
                view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
            }
        }
    

提交回复
热议问题