Android 全面屏黑邊適配
問題:
全面屏底部會出現黑邊
原因:
全面屏寬高比例比較特殊,不是之前的16:9。例如小米mix2s,螢幕寬高比是18:9。Android預設支援的最大寬高比例是1.86,小於全面屏手機的寬高比例。所有如果全面屏手機上的應用沒有做適配,底部就會出現黑邊。
解決方案:
- 方案1:設定resizeableActivity屬性值
<application ... android:resizeableActivity="true">
targetSdkVersion 24及以上預設為true,可以不用設定該屬性值。同時該方案會開啟應用分屏功能,注意介面適配問題。
-
方案2:設定android.max_aspect屬性值 (建議 )
建議設為2.1或更大,因為18:9=2,以後出現寬高比更大的手機屏,需要將這個值設定的更大
設定方法:
1.在AndroidManifest的Application標籤下面增加下面一段程式碼
<meta-data android:name="android.max_aspect" android:value="2.3"/>
2.程式碼動態設定max_aspect值:
ApplicationInfo applicationInfo = null; try { applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if(applicationInfo == null){ throw new IllegalArgumentException(" get application info = null, has no meta data! "); } applicationInfo.metaData.putString("android.max_aspect", "2.3");
參考文章:
Android APP適配全面屏手機的技術要點