我们在点开app 的时候会有一会时间的摆平活着黑屏的现象出现,虽然时间不长,但是还是很影响用户的体验,一般我们会给window加一个和启动页一样的背景,这样用户就看不出来有白屏或者黑屏了
先在drawable文件下写定义我们需要的背景
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景颜色 -->
<item android:drawable="@color/white" />
<!--
<item
android:gravity="center">
<shape android:shape="rectangle" >
<size android:height="30dp"
android:width="30dp" />
<solid android:color="@color/white" />
</shape>
</item>-->
<item>
<!-- 图片 -->
<bitmap
android:width="153dp"
android:height="71dp"
android:gravity="center"
android:src="@mipmap/login_img_logo" />
</item>
</layer-list>然后再style文件下引用我们的背景<style name="SplashTheme" parent="AppTheme">
<!-- 启动页背景 -->
<item name="android:windowBackground">@drawable/splash_bg</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">false</item> <!-- 透明背景 -->
</style>
最后对我们的启动页设置主题为该主题<activity android:name="activities.MainSplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
你以为这样就大功告成了 不 对于华为的手机我们会发现背景和我们启动页的背景不能一致 这是因为华为手机有导航栏
因此我们还需要将导航栏设置为半透明的
StatusBarUtils.hintStautsBar(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//设置半透明导航栏适配华为手机
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
super.onCreate(savedInstanceState);
initViews();
}