至于全屏展示,就得做适配工作,有以下两种方式可进行适配:
android:MaxAspectRatio
来声明其支持的屏幕最大宽高比。<!-- Render on full screen up to screen aspect ratio of 2.4 --> <!-- Use a letterbox on screens larger than 2.4 --> <activity android:maxAspectRatio="2.4"> ... </activity>
android.max_aspect
的 元素如下所示:
<!-- Render on full screen up to screen aspect ratio of 2.4 --> <!-- Use a letterbox on screens larger than 2.4 --> <meta-data android:name="android.max_aspect" android:value="2.4" />
Google 为刘海屏显示方式提供了三种显示模式:
// 默认情况,全屏页面不可用刘海区域,非全屏页面可以进行使用 public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT = 0; // 允许页面延伸到刘海区域 public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES = 1; // 不允许使用刘海区域 public static final int LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER = 2;
我们可以通过下面两种方式来指定应用在凹形屏幕的显示模式:
android:windowLayoutInDisplayCutoutMode
属性指定显示模式:// value-v28/styles.xml <style name="AppTheme.Launcher" parent="AppTheme"> <item name="android:windowBackground">@drawable/branded_launch_screens</item> <item name="android:statusBarColor">@color/colorPrimary</item> <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> </style>
我们可以在 Activity 的 onCreate 中指定凹形屏幕的显示模式:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= 28) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; getWindow().setAttributes(lp); } }
具体使用:需要在values-v27及以上的styles.xml中加入以下主题设置:
<!--实现启动页全屏--> <style name="Theme.SplashActivity" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@color/white</item> <item name="android:windowTranslucentStatus">false</item> <item name="android:statusBarColor">@android:color/transparent</item> <item name="colorPrimary">@color/main_bg</item> <item name="colorPrimaryDark">@color/white</item> <item name="colorAccent">@color/white</item> <item name="android:windowFullscreen">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowTranslucentNavigation">true</item> <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> </style>