在Android 14及更高版本中,可以通过设置WindowInsets来处理应用程序顶部的摄像头凹槽/菜单栏,以避免出现黑色的条。
首先,在你的Activity的onCreate方法中,设置WindowInsetsController属性:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController insetsController = getWindow().getInsetsController();
if (insetsController != null) {
insetsController.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
}
}
然后,在你的Activity的onWindowInsetsChanged方法中,处理WindowInsets的变化:
@Override
public void onWindowInsetsChanged(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController insetsController = getWindow().getInsetsController();
if (insetsController != null) {
// 隐藏状态栏
insetsController.hide(WindowInsets.Type.statusBars());
// 处理摄像头凹槽/菜单栏
insetsController.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);
}
}
}
确保你的Activity继承自AppCompatActivity,并且在AndroidManifest.xml文件中设置android:windowFullscreen属性为true:
这样,你的应用程序顶部的摄像头凹槽/菜单栏将被正确处理,黑色的条应该会消失。