1.设置主题:在styles里面添加主题【挖孔屏的话,有一个地方需要注意一下,在最后面】
<style name="AppTheme_NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/sp_loading</item>
</style>
重点:@drawable/sp_loading 这个,之前我这个只是一张图片,后来发现在有的机型上面会变形拉伸,效果很差,现在这里变成了一个xml文件,就不会有这种问题了
<?xml version="1.0" encoding="utf-8"?><!--
//使用layer-list添加多个图层,返回LayerDrawable
//注意:bitmap使用的图片大小要小于屏幕大小
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:src="@mipmap/windownsbg" />
</item>
<item android:top="@dimen/dp_80">
<bitmap
android:layout_width="@dimen/dp_120"
android:layout_height="@dimen/dp_120"
android:gravity="top"
android:src="@mipmap/img_splash_logo" />
</item>
<item android:top="220dp">
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:src="@mipmap/img_splash_item1" />
</item>
<item android:top="280dp">
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:src="@mipmap/img_splash_item2" />
</item>
<item android:bottom="@dimen/dp_80">
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:src="@mipmap/img_splash_item3" />
</item>
</layer-list>
扩展:如果在styles.xml文件中将启动页窗口背景图片(android:windowBackground)设置为欢迎界面的背景图片,然后欢迎界面布局文件中将背景(android:background)设置为透明,Activity中不恢复原有的样式,那么这样就可以实现APP启动后白屏部分和欢迎界面是同一张背景图片。
需要注意,如果将之前欢迎界面的背景图片作为窗口背景图片,那么就需要考虑到底部导航栏的高度的问题。否则背景图片的底部会被遮盖住。所以可能需要重新调整欢迎界面的背景图片。
2.给启动页设置主题
挖孔屏需要注意一个地方!特别是启动页!!
有可能你们会遇到这样一种情况,闪屏页刚刚进来的一瞬间是全屏显示的,但是等了一会,发现上面标题栏那里有一个黑边,然后页面会被稍微往下压缩一下,这是因为没有适配挖孔屏的缘故,两种方式解决:
第一:在 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);
}
}
第二:在主题中加入android:windowLayoutInDisplayCutoutMode
属性指定显示模式:
// value-v28/styles.xml
<style name="NoTitleFullscreen" 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>