系統(tǒng)自帶app和很多第三方應(yīng)用都有此現(xiàn)象。
實(shí)際上是因?yàn)閙anifest添加了 android:screenOrientation="portrait"
屬性,導(dǎo)致LetterBox顯示。顯示具體log如下
D/ActivityTaskManager: Show LetterBox: Window{820f20 u0 Splash Screen com.android.myapplication}, reason=FIXED_ORIENTATION
通過(guò)winscope也能看出這個(gè)是LetterBox。
簡(jiǎn)單說(shuō)下問(wèn)題的由來(lái)。
adb shell wm size
Physical size: 720x720
首先判斷當(dāng)前設(shè)備是橫屏還是豎屏。
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 橫屏
所以,由于設(shè)備是橫屏,但是manifest 添加了
android:screenOrientation="portrait",強(qiáng)制豎屏,
因此FIXED_ORIENTATION,觸發(fā)了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; //固定開(kāi)啟resizeable屬性 ,在當(dāng)前設(shè)備上無(wú)法解決黑邊問(wèn)題:https://blog.csdn.net/HuanWen_Cheng/article/details/141966932
這種情況下,隱藏LetterBox(這里我嘗試修改com.android.server.wm.LetterboxUiController#shouldShowLetterboxUi,直接返回false)也會(huì)顯示不全,所以我修改了resolveFixedOrientationConfiguration 里的數(shù)據(jù)。
參考這個(gè):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);
}*/
// 強(qiáng)制 desiredAspectRatio 為 0,這樣 applyAspectRatio 幾乎什么都不會(huì)做。
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 模式
強(qiáng)制app橫屏顯示或者豎屏顯示(動(dòng)態(tài))
Android之LetterBox介紹
Android11.0(R) MTK6771 平板橫屏方案修改(強(qiáng)制app橫屏 + 開(kāi)機(jī)logo/動(dòng)畫(huà)+關(guān)機(jī)充電橫屏 + RecoveryUI 橫屏)
Android 10 應(yīng)用顯示寬高比maxAspectRatio導(dǎo)致應(yīng)用區(qū)域顯示不全的問(wèn)題
Android全屏黑邊解決方案
手機(jī)app在平板上沒(méi)有全屏顯示
關(guān)于A(yíng)ndroid12第三方應(yīng)用左右兩側(cè)有黑邊問(wèn)題
Android 12.0 第三方應(yīng)用左右兩側(cè)未全屏有黑邊問(wèn)題解決
Android P應(yīng)用顯示寬高比maxAspectRatio使用及原理