如何根据android中的某些条件设置状态栏颜色并将其重置为原始颜色

时间:2016-03-22 13:28:22

标签: android statusbar

我想更改状态栏的颜色,并且与指定的here相同。我有以下疑惑。 1)但我想根据某些条件将状态栏颜色重置为原始颜色,我将如何做到这一点。 我试过这样的。

public class StoreDisableHelper {

    public static void changeStatusBarColor(boolean status,Activity activity){
        Window window = activity.getWindow();
        if(status){


// clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(activity.getResources().getColor(R.color.theme_red));
            }

        }
        else{
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }

    }
}

当条件为假时,我正在重置

1 个答案:

答案 0 :(得分:1)

这个:

https://gist.github.com/MrThiago/4897424c3c773aec57081325f273311a

private static int defaultStatusBarColor;

public static void changeStatusBarColor(Activity context, boolean change, int color){
    if (Build.VERSION.SDK_INT >= 21)
    {
        Window window = context.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        if(change){
            defaultStatusBarColor = window.getStatusBarColor();
            window.setStatusBarColor(color);
        }
        else{
            // reset
            window.setStatusBarColor(defaultStatusBarColor);
        }
    }
}