如何自定义Android状态栏颜色

// 定义颜色值
private int[] statusColors = new int[]{R.color.color1_, R.color.color2_, 
R.color.color3_, R.color.color4_, R.color.color5_};
 // 设置Theme
 setCurrentTheme(ContextCompat.getColor(MainActivity.this, statusColors[currentColor]));
 private void setCurrentTheme(int statusColor) {
       if (Build.VERSION.SDK_INT >= 21) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getWindow().setStatusBarColor(statusColor);
          
        } else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | 
                                                localLayoutParams.flags);

            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            View statusBarView = contentView.getChildAt(0);
            if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(this)) {
                statusBarView.setBackgroundColor(statusColor);
                return;
            }
            statusBarView = new View(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    getStatusBarHeight(this));
            statusBarView.setBackgroundColor(statusColor);
            contentView.addView(statusBarView, lp);
        }
}
       
// values-v19
 
//values-v21
    
   

你可能感兴趣的:(如何自定义Android状态栏颜色)