1

I have created a function as below which sets fitSystemWindow attribute :

private void setFitSystemWindows() {
        try {
            if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
                // Do something for lollipop and above versions
                View view = findViewById(R.id.relRoot);
                view.setFitsSystemWindows(true);
                view.setPadding(0, 0, 0, 0);

                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(ContextCompat.getColor(CameraActivity.this, R.color.colorBlackTransperent));
            } else {
                // do something for phones running an SDK before lollipop
                View view = findViewById(R.id.relRoot);
                view.setFitsSystemWindows(false);
                view.setPadding(0, 0, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Let me explain it :

Here, R.id.relRoot is the parent root RelativeLayout in layout xml file.

I am checking build version first and If build version is greater than Lollipop, am setting fitsSystemWindows to true and if not then setting it to false.

But, still when I run the application in devices > Lollipop, am getting my status bar color white and bottom soft view (which contains back,home and recent) also with the color white.

You can see I am also using method setStatusBarColor but it's not working.

What might be the issue ?

NOTE : Am checking in emulator device : NEXUS 5X API 27 (Android 8.1.0, API 27)

4

2 回答 2

4

在这里,我使用已实现的此功能设置状态栏颜色。在 Lollipop 中,我将状态栏颜色设置为黑色。在上面的版本中,我设置了我的应用程序主题颜色。

功能:-

fun setStatusBarColor(activity: Activity, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.window.statusBarColor = ContextCompat.getColor(activity, color)
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
                activity.window.statusBarColor = ContextCompat.getColor(activity, android.R.color.black)
            }
        }
    }

注意:-我在 kotlin 中实现了上述功能。

Java函数:-

public void setStatus(Activity activity, int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, color));
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1)
                activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, android.R.color.black));
        }
    }
于 2019-09-12T05:16:48.697 回答
1

好吧,我试过你的代码,它对我有用。所以我只能尝试分析您的代码片段。

  1. 调试器说什么?您是否尝试在方法旁边设置标记?调试器去哪儿了?也许调试器出于某种原因跳入异常。

  2. 正确,如果:

if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.LOLLIPOP) {
    // Do something for lollipop and above versions
}

您不是在寻找 lollipop 及以上版本,而是在寻找以上版本,除了 lollipop。如果您也想寻找棒棒糖,请使用>=

  1. 检查颜色。您是否尝试设置另一种颜色(不透明)?

  2. 语境。你试过getApplicationContext()或类似的吗?

我建议您使用调试器来查找问题。也许它与代码有关,也许与颜色有关。正如我所说,我尝试了您的代码,它对我有用。

于 2019-09-10T13:41:07.040 回答