WindowInsetsController的使用
时间: 2023-03-27 16:05:04 浏览: 70
WindowInsetsController 是 Android 11 新增的一个类,用于控制窗口的边距和系统栏的显示。可以通过 WindowInsetsController 的方法来设置窗口的边距和系统栏的显示状态,从而实现更加灵活的界面布局和交互效果。
相关问题
安卓12实现windowInsetsController隐藏导航栏且无法被唤起
可以使用WindowInsetsController的hide方法隐藏导航栏,同时可以使用setSystemBarsBehavior方法将系统栏的行为设置为SYSTEM_UI_FLAG_IMMERSIVE_STICKY,这样即使用户滑动屏幕,也无法唤起导航栏。具体实现可以参考以下代码:
```java
View decorView = getWindow().getDecorView();
WindowInsetsController insetsController = decorView.getWindowInsetsController();
insetsController.hide(WindowInsets.Type.navigationBars());
insetsController.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
```
需要注意的是,这些方法只在Android 12及以上版本中可用,如果需要在低版本上实现类似的效果,可以考虑使用全屏模式或者在布局中覆盖导航栏。
CarSystemUI 如何监听 有应用使用了setStatusBarColor
在Android中,可以通过`WindowInsetsController.OnControllableInsetsChangedListener`接口监听窗口可控制Insets的变化。在Android 11及以上的版本中,状态栏的背景色可以通过`Window.setStatusBarColor(int color)`或`Window.setDecorFitsSystemWindows(boolean decorFitsSystemWindows)`方法设置。因此,可以通过监听窗口可控制Insets的变化,来判断应用是否使用了`setStatusBarColor()`方法或`setDecorFitsSystemWindows()`方法设置了状态栏背景色。
具体做法如下:
1. 在Activity中添加一个`WindowInsetsController.OnControllableInsetsChangedListener`对象:
```java
private WindowInsetsController.OnControllableInsetsChangedListener mInsetsChangedListener =
new WindowInsetsController.OnControllableInsetsChangedListener() {
@Override
public void onControllableInsetsChanged(WindowInsetsController controller, int type) {
if ((type & WindowInsetsCompat.Type.statusBars()) != 0) {
WindowInsetsCompat insets = controller.getInsets(WindowInsetsCompat.Type.statusBars());
if (insets.isVisible() && insets.getSystemWindowInsetTop() > 0) {
// 应用使用了setStatusBarColor()方法设置了状态栏背景色
}
}
}
};
```
2. 在Activity的`onCreate()`方法中,获取当前窗口的`WindowInsetsController`对象,并为其设置监听器:
```java
WindowInsetsController insetsController = getWindow().getInsetsController();
if (insetsController != null) {
insetsController.addOnControllableInsetsChangedListener(mInsetsChangedListener);
}
```
3. 在`onControllableInsetsChanged()`方法中,处理窗口可控制Insets的变化事件。当应用使用了`setStatusBarColor()`方法或`setDecorFitsSystemWindows()`方法设置了状态栏背景色时,窗口的顶部Insets会发生变化,此时可以通过如下代码判断:
```java
if (insets.isVisible() && insets.getSystemWindowInsetTop() > 0) {
// 应用使用了setStatusBarColor()方法设置了状态栏背景色
}
```
其中,`insets.isVisible()`方法用于判断Insets是否可见,`insets.getSystemWindowInsetTop()`方法用于获取Insets的顶部高度。如果Insets可见且顶部高度大于0,则说明应用使用了`setStatusBarColor()`方法或`setDecorFitsSystemWindows()`方法设置了状态栏背景色。
需要注意的是,`WindowInsetsController.OnControllableInsetsChangedListener`接口是在Android 11及以上版本中引入的,因此只有在Android 11及以上版本中才能使用该接口。在Android 10及以下版本中,无法通过该接口监听应用是否使用了`setStatusBarColor()`方法或`setDecorFitsSystemWindows()`方法设置了状态栏背景色。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)