在Android 12上,系统实施了安全性更高的UI变更,因此无法在覆盖层下方进行点击或滚动。为了解决这个问题,您可以尝试以下两种方法:
方法一:使用WindowInsets
您可以使用WindowInsets API来监听窗口的insets,并将其应用于您的View,以确保其正确处理覆盖层。如下所示:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
yourView.setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
@Override
public WindowInsets onProgress(WindowInsets insets, List
// Set top padding of yourView to the top inset of the window.
yourView.setPadding(0, topInset, 0, 0);
// Return the same insets to prevent changes to the view hierarchy.
return insets;
}
});
}
方法二:使用setOnApplyWindowInsetsListener
另一种方法是使用setOnApplyWindowInsetsListener来监听窗口的insets,并在View中应用它们。如下所示:
ViewCompat.setOnApplyWindowInsetsListener(yourView, new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { // Get the top inset of the current window from WindowInsetsCompat. Insets windowInsets = insets.getInsets(WindowInsetsCompat.Type.navigationBars()); int topInset = windowInsets.top;
// Set top padding of yourView to the top inset of the window.
yourView.setPadding(0, topInset, 0, 0);
// Return the modified insets to consume them.
return insets.consumeSystemWindowInsets();
}
});
请注意,这些解决方法可能需要对您的代码进行一些更改。这些解决方案中提供的例子将根据需要调整窗口的padding,但是您可以根据您的应用程序需要进行调整。
上一篇:Android12上无法通过USB线缆端口连接USB数位板,但在Android10上可以正常运行。
下一篇:Android12上小米11Ultra设备出现“openfailed:EACCES(Permissiondenied)”错误。