Code for setting light status bar in android 11 and below android 11 deprecation warning not going

huangapple go评论78阅读模式
英文:

Code for setting light status bar in android 11 and below android 11 deprecation warning not going

问题

Window window = MainActivity.this.getWindow();
window.setStatusBarColor(MY_COLOR_IT_CAN_BE_ANY);

if (Build.VERSION.SDK_INT < 30)
{
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
else {
    window.setDecorFitsSystemWindows(false);
    WindowInsetsController controller = getWindow().getInsetsController();
    if(controller != null) {
        controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
    }
}
英文:
 Window window = MainActivity.this.getWindow();
                   window.setStatusBarColor(MY_COLOR_IT_CAN_BE_ANY);

                   if (Build.VERSION.SDK_INT &lt; 30)
                   {
                       window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                   }
                   else {
                       window.setDecorFitsSystemWindows(false);
                       WindowInsetsController controller = getWindow().getInsetsController();
                       if(controller != null) {
                           controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                                   WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
                       }
                   }

I am using this code for setting light status bar in android 11 and below android 11. Everything works fine, just a little problem, deprecation warning not going.

答案1

得分: 0

弃用警告是在您使用弃用的代码时出现的,而您实际上没有——您有适当的 if 语句,导致在 < 30 上使用尚未弃用的方法和在 30+ 上使用新方法。

英文:

deprecation warning is showing when you use deprecated code and you are not - you have proper if statemnent, causing using not-yet-deprecated methods on &lt; 30 and new method on 30+

答案2

得分: 0

if (fullScreen) {
    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
} else {
    getWindow().getDecorView().getWindowInsetsController().show(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_BARS_BY_SWIPE);
}

以上代码用于在 Android 11 中实现全屏显示,重要的是要设置setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE),否则在向下滑动状态栏时,状态栏会显示出来并且不会在活动重新创建之前消失。

英文:
if(fullScreen)
{
    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
else
{
    getWindow().getDecorView().getWindowInsetsController().show(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_BARS_BY_SWIPE);
}

Above code used for full screen from Android 11, it's important to set setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE) else on swipe down status bar get display and does not disappear till activity is not recreated.

答案3

得分: -1

我只做了一件事情,因为我的条件语句是正确的,所以我只是通过使用

@SuppressWarnings("deprecation")

来抑制警告。

我只是在包含这段代码的特定方法中使用了这个注解。

还有另一个选项也是完全可行的:
只需在代码的上面添加

//noinspection deprecation

这行代码是过时的。这将允许检查该整个函数中的其他警告。这是很好的,而且我会更倾向于这样做。

相较于 @SuppressWarnings("deprecation"),//noinspection deprecation 有什么优势呢?

这可以避免 @SuppressWarnings 的问题,它会忽略方法中的所有警告。因此,如果你使用了一些你不知道的已过时的内容,@SuppressWarnings 会隐藏它,你将不会收到警告。这就是 //noinspection 的优势。

更多细节请参阅此帖子的答案:
https://stackoverflow.com/questions/24208410/how-to-suppress-specific-lint-warning-for-deprecated-android-function

英文:

I just did one thing, since my if statement is fine, I just suppressed warning by using
@SuppressWarnings("deprecation")

I just used this annotation for that particular method which contains this code.

There is another option present which is also perfectly fine:
just add

//noinspection deprecation

above line of code which is deprecated. This will allow to check other warnings in that whole function. Which is good and I will prefer.

What is the advantage of //noinspection deprecation over @SuppressWarnings("deprecation")?

This prevents the problem with @SupressWarnings, which is it ignores all warnings in the method. So if you have something deprecated that you are not aware of, @SupressWarnings will hide it and you will not be warned. That is the advantage of the //noinspection

Courtest and check more details in this post answers:
https://stackoverflow.com/questions/24208410/how-to-suppress-specific-lint-warning-for-deprecated-android-function

huangapple
  • 本文由 发表于 2020年9月14日 14:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63878935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定