Android 启动页全屏适配

1,097 阅读1分钟

问题

最近测试提了一个bug,个别手机比如华为荣耀8、小米mix2,点击App启动的时候,底部导航栏一直显示的黑色的,与App白色主题完全不符,希望能隐藏或修改导航栏颜色为白色。

Step 1

刚开始是想在SplashActivity启动页的onCreate里面使用ImmersionBar隐藏导航栏,

ImmersionBar.with(this)
            .reset()
            .statusBarColor(R.color.topbarbgcolor)
            .navigationBarColor(R.color.topbarbgcolor)
            .statusBarDarkFont(true, 0.2f)
            .navigationBarDarkIcon(true, 0.2f)
            .fitsSystemWindows(true)
            .init();
            

当然,这是完全没有用的,因为SplashActivity背景只有一张图片,onCreate执行时机已经很晚了,所以还是在style文件下点功夫。

Step 2

设置SplashActivity的theme样式,

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_bg</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

运行无效

Step3

既然需求不想显示状态栏和导航栏,那windowFullscreen属性必不可少,有没有方法能修改二者的颜色呢,还真有:

 <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_bg</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

运行会发现状态栏隐藏、导航栏虽然没隐藏,但是颜色可以自己改变了,比之前算是改进了不少,另外windowDrawsSystemBarBackgrounds加上会导致图片变形或者导航栏更改颜色失效,所以就去掉了。还真是会遇到各种奇葩的问题。

至此,告一段落