在Android 11之前,我已经轻松地将我的应用程序调整为全屏
我的旧手机的摄像头孔和底座按钮位于屏幕区域之外,我的新手机的摄像头孔和底座按钮位于屏幕区域内。
通过一些设置,我的应用程序在旧手机中全屏显示。
Styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
AndroidManifest.xml
<application
...
android:theme="@style/AppTheme.NoActionBar">
</application>
在装有Android 11的新手机中,我搜索了许多
Stackoverflow
问题。我尝试了很多不同的解决方案。 最后,我将以下代码放入我的应用程序中 OnCreate()
的开头。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowCompat.setDecorFitsSystemWindows(window, false)
else {
@Suppress("DEPRECATION")
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
我想要靠近基本按钮和摄像头孔的边缘很窄,就像 Chrome 一样。
但我所能得到的只是应用程序和按钮(向下)以及应用程序和相机孔(向上)之间的宽边距:
但我想要一个窄边距:
类似于 Android 版 Chrome 浏览器:
我如何以编程方式执行此操作?
我无法在互联网(和 StackOverflow)中找到解决我的问题的任何线索
我也无法弄清楚如何识别给定手机的屏幕上是否有摄像头孔或者屏幕上是否有基本按钮。看起来是基于
DisplayCutout
、WindowInsets
和boundingRectTop
和boundingRectBottom
,但是网上没有真实清晰的使用示例。
相关行是:
<meta-data android:name="android.max_aspect" android:value="2.4"/>
2.4 是屏幕中应用程序区域允许的最大
screen height / screen width
。
如果省略此声明,系统仍然假设最大纵横比为1.86。
如果当今许多设备的比率
screen height /screen width
大于1.86,我不知道为什么系统会假设默认值如此之低。
从我在问题中所写的内容来看,确实需要更改 Android 的标准样式之一
所以在
styles.xml
:
<style name="AppTheme"> parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
在
AndroidManifest.xml
中包括
<application
...
android:theme="@style/AppTheme">
<meta-data android:name="android.max_aspect" android:value="2.4"/>
...
<\application>
这一切......
我在这个主题上找到的唯一远程参考是在this帖子的2017年页面上的开发者官方博客上。