版权声明:本文为HaiyuKing原创文章,转载请注明出处!android
StartingWindow 的处理方式:git
-------摘自《知乎 救救你的 StartingWindow》github
android开发者应该都有这样的体会:开发到必定的阶段,包变得很大了,每次启动APP的时候,老是有白屏一闪而过(白屏的时间和包的大小成正比!)。浏览器
-------摘自《Android APP启动白屏优化》微信
本文讲的是第二种处理方式。app
暂无ide
注意事项:布局
一、 导入类文件后须要change包名以及从新import R文件路径优化
二、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),若是项目中存在,则复制里面的内容,不要整个覆盖动画
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 应用启动页(StartingWindow)的theme --> <style name="AppTheme.StartingWindowTheme" parent="AppTheme"> <!-- 能够设置成纯颜色(设置一个和Activity UI类似的背景) --> <!--<item name="android:windowBackground">@color/startingwindow_bgcolor</item>--> <!--也能够设置成一张图片 --> <item name="android:windowBackground">@drawable/startingwindow_bg</item> </style> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- 应用启动页(StartingWindow)的theme的背景色 --> <color name="startingwindow_bgcolor">#00bfff</color> </resources>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.androidstartingwindowdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!--将首页的them设置成自定义的样式--> <activity android:name=".MainActivity" android:theme="@style/AppTheme.StartingWindowTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
package com.why.project.androidstartingwindowdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.AppTheme);//恢复原有的样式 setContentView(R.layout.activity_main); } }
扩展:若是在styles.xml文件中将启动页窗口背景图片(android:windowBackground)设置为欢迎界面的背景图片,而后欢迎界面布局文件中将背景(android:background)设置为透明,Activity中不恢复原有的样式,那么这样就能够实现APP启动后白屏部分和欢迎界面是同一张背景图片。
须要注意,若是将以前欢迎界面的背景图片做为窗口背景图片,那么就须要考虑到底部导航栏的高度的问题。不然背景图片的底部会被遮盖住。因此可能须要从新调整欢迎界面的背景图片。
无