系统自带app和很多第三方应用都有此现象。
实际上是因为manifest添加了 android:screenOrientation="portrait"
属性,导致LetterBox显示。显示具体log如下
D/ActivityTaskManager: Show LetterBox: Window{820f20 u0 Splash Screen com.android.myapplication}, reason=FIXED_ORIENTATION
通过winscope也能看出这个是LetterBox。
简单说下问题的由来。
adb shell wm size
Physical size: 720x720
首先判断当前设备是横屏还是竖屏。
val configuration: Configuration = resources.configuration
val orientation: Int = configuration.orientation
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// 竖屏
Log.d("MainActivity", "竖屏")
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 横屏
Log.d("MainActivity", "横屏")
}
log显示是横屏。
MainActivity com.android.myapplication D 横屏
所以,由于设备是横屏,但是manifest 添加了
android:screenOrientation="portrait",强制竖屏,
因此FIXED_ORIENTATION,触发了Letterbox显示流程。
// com.android.server.wm.LetterboxUiController#layoutLetterbox
if (!Build.IS_USER) {
Slog.d(TAG,"Show LetterBox: " + w.toString()
+ ", reason=" + getLetterboxReasonString(w));
}
按这种方式去修改 int resizeMode = ActivityInfo.RESIZE_MODE_RESIZEABLE; //固定开启resizeable属性 ,在当前设备上无法解决黑边问题:https://blog.csdn.net/HuanWen_Cheng/article/details/141966932
这种情况下,隐藏LetterBox(这里我尝试修改com.android.server.wm.LetterboxUiController#shouldShowLetterboxUi,直接返回false)也会显示不全,所以我修改了resolveFixedOrientationConfiguration 里的数据。
参考这个:https://blog.csdn.net/wq892373445/article/details/123863686
我直接修改为final float desiredAspectRatio = 0.0f;,注释掉原本的逻辑。
// com.android.server.wm.ActivityRecord#resolveFixedOrientationConfiguration
// Aspect ratio as suggested by the system. Apps requested mix/max aspect ratio will
// be respected in #applyAspectRatio.
/* final float desiredAspectRatio;
if (isDefaultMultiWindowLetterboxAspectRatioDesired(newParentConfig)) {
desiredAspectRatio = DEFAULT_LETTERBOX_ASPECT_RATIO_FOR_MULTI_WINDOW;
} else if (letterboxAspectRatioOverride > MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO) {
desiredAspectRatio = letterboxAspectRatioOverride;
} else {
desiredAspectRatio = computeAspectRatio(parentBounds);
}*/
// 强制 desiredAspectRatio 为 0,这样 applyAspectRatio 几乎什么都不会做。
final float desiredAspectRatio = 0.0f;
// Apply aspect ratio to resolved bounds
mIsAspectRatioApplied = applyAspectRatio(resolvedBounds, containingBoundsWithInsets,
containingBounds, desiredAspectRatio);
使用 adb shell dumpsys activity -a a com.android.myapplication
可以看到修改desiredAspectRatio值后activityAspectRatio值变化:
areBoundsLetterboxed=true
letterboxReason=FIXED_ORIENTATION
activityAspectRatio=1.1483253
areBoundsLetterboxed=true
letterboxReason=FIXED_ORIENTATION
activityAspectRatio=1.0
参考链接:
Android 12 - Letterbox 模式
强制app横屏显示或者竖屏显示(动态)
Android之LetterBox介绍
Android11.0(R) MTK6771 平板横屏方案修改(强制app横屏 + 开机logo/动画+关机充电横屏 + RecoveryUI 横屏)
Android 10 应用显示宽高比maxAspectRatio导致应用区域显示不全的问题
Android全屏黑边解决方案
手机app在平板上没有全屏显示
关于Android12第三方应用左右两侧有黑边问题
Android 12.0 第三方应用左右两侧未全屏有黑边问题解决
Android P应用显示宽高比maxAspectRatio使用及原理