Android 状态栏背景颜色修改与状态栏字体颜色修改

 public class StatusBarUtils {
    /**
     * 修改状态栏为沉浸式,并修改背景颜色与字体颜色
     *
     * @param activity
     */
    @TargetApi(19)
    public static void transparencyBar(Activity activity,int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            
            //View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN设置为沉浸式
            //View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 设置字体颜色为深色
            //View.SYSTEM_UI_FLAG_VISIBLE 设置字体颜色为浅色
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //这里设置背景颜色,也可以设置成为透明色
            window.setStatusBarColor(color);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
 /** 
     * 设置状态栏背景颜色 且根据背景颜色修改状态栏字体的颜色 
     */
public static void setStatusBar(@ColorInt int color) {  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  

            // 设置状态栏底色颜色  
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
            getWindow().setStatusBarColor(color);  

            // 如果亮色,设置状态栏文字为黑色  
            if (isLightColor(color)) {  
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);  
            } else {  
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);  
            }  
        }  

    }  

    /** 
     * 判断颜色是不是亮色 
     * 
     * @param color 
     * @return 
     * @from https://stackoverflow.com/questions/24260853/check-if-color-is-dark-or-light-in-android 
     */  
    private static boolean isLightColor(@ColorInt int color) {  
        return ColorUtils.calculateLuminance(color) >= 0.5;  
    }  

    /** 
     * 获取StatusBar颜色,默认白色 
     * 
     * @return 
     */  
    protected static @ColorInt int getStatusBarColor() {  
        return Color.WHITE;  
    }  

   
}

你可能感兴趣的:(Java,Android)