解决android在启动闪屏页面前会出现短暂的白屏或黑屏状态

来源:互联网 发布:人类返祖现象特征知乎 编辑:程序博客网 时间:2024/06/08 15:30

先直接上代码:

第一步:
自定义style ,一般也设置闪屏页面为竖屏.

    <activity android:name=".SplashActivity"                android:theme="@style/splashtheme"                    android:screenOrientation="portrait">        <intent-filter>            <action android:name="android.intent.action.MAIN"              />            <category android:name="android.intent.category.LAUNCHER" />        </intent-filter>     </activity>

第二步:
在res/values/styles中自定义的splashtheme设置parent为 android:Theme.NoTitleBar.Fullscreen,并设置windowBackground图片

<style name="NoAnimationTheme" parent="android:Theme.NoTitleBar.Fullscreen">    <item name="android:windowBackground">@drawable/splash_logo</item>    <item name="android:windowAnimationStyle">@style/Animation</item></style>

注意:
设置属性是windowbackground不是background否则会显示混乱

白屏和黑屏闪现原因分析:
若设置logo图片在activity_splash.xml文件中,在启动Splash_Activity执行setContentView(R.layout.activity_splash);渲染完成后才能看到logo图片,而显示这个张图片前会默认加载application 的主题样式android:theme=@style/AppTheme 的主题导致.

拓展:
application下有一个默认主题
android:theme=”@style/AppTheme”
按住ctrl点击去找到AppTheme找到AppTheme,在里面添加属性windowBackground,代码如下

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">    <!-- Customize your theme here. -->    <item name="android:windowBackground">@drawable/mybackground</item>    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    <item name="colorAccent">@color/colorAccent</item></style>

试验发现开启app的时候仍然会闪现我们的logo图片,但是进入activity后,你会发现所有的acitivity都是一样的图片背景,如果设置acitivity自己的theme,就不会用该图片背景.

0 0
原创粉丝点击