[英]Android R : API 30 : System bar visibility change call backs
I am trying to make my app a full screen app.我正在尝试使我的应用程序成为全屏应用程序。 On Api levels 29 and below, I am using
onSystemUIVisibilityChange
listener to handle visibility of certain views in my activity when the app is in full screen.在 Api 级别 29 及以下,我使用
onSystemUIVisibilityChange
侦听器在应用程序全屏时处理我的活动中某些视图的可见性。 But on api level 30, is there any similar callback to know when system bars(status bar and navigation bar) visibility changes?但是在 api 级别 30 上,是否有类似的回调来了解系统栏(状态栏和导航栏)的可见性何时发生变化? Since
onSystemUIVisibilityChange
is deprecated for API level 29 and above.由于 API 级别 29 及更高版本不推荐使用
onSystemUIVisibilityChange
。
I am aware of activity.window.insetsController.show(WindowInsets.Type.systemBars())
to show and hide System bars.我知道
activity.window.insetsController.show(WindowInsets.Type.systemBars())
来显示和隐藏系统栏。 What I need is a callback when system bar visibility changes.我需要的是系统栏可见性改变时的回调。
As mention in the documentation you can set a listener using正如文档中所述,您可以使用设置侦听器
View.OnApplyWindowInsetsListener((view: View, window:WindowInsets) -> WindowInsets)
and then in the listener you can check whether the system bars are visible or not using WindowInsets.isVisible(Int)
.然后在侦听器中,您可以使用
WindowInsets.isVisible(Int)
检查系统栏是否可见。 Like so:像这样:
private val onWindowInsetApplied: (view: View, window:WindowInsets) -> WindowInsets = { view, windowInsets ->
val areSystemBarsVisible = windowInsets.isVisible(WindowInsets.Type.systemBars())
//Do something
windowInsets
}
fun setListener() {
View.OnApplyWindowInsetsListener(onWindowInsetApplied)
}
For Java developers, this is the java
version of @ Kyzer Soze
answer对于 Java 开发人员,这是
@ Kyzer Soze
答案的java
版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isVisible(WindowInsets.Type.systemBars())) {
Toast.makeText(MainActivity.this, "system bars are Visible",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "system bars are Invisible",
Toast.LENGTH_SHORT).show();
}
return insets;
}
});
}
Make sure not to call setOnApplyWindowInsetsListener
in below API-30, as it will raise runtime exception确保不要在 API-30 以下调用
setOnApplyWindowInsetsListener
,因为它会引发运行时异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.