Android 启动器活动setContentView延迟
我创建了一个简单的应用程序,启动屏幕显示1秒。闪屏几乎是红色的。但我注意到,当我启动应用程序时,最初约0.3秒会出现白色屏幕,之后会出现启动屏幕。是否可以删除此白色屏幕或使其变为红色?我用android 5.0在nexus 4上进行了测试,我的Splash活动的实现非常简单,没有任何东西可以延迟onCreate():Android 启动器活动setContentView延迟,android,android-activity,activity-lifecycle,Android,Android Activity,Activity Lifecycle,我创建了一个简单的应用程序,启动屏幕显示1秒。闪屏几乎是红色的。但我注意到,当我启动应用程序时,最初约0.3秒会出现白色屏幕,之后会出现启动屏幕。是否可以删除此白色屏幕或使其变为红色?我用android 5.0在nexus 4上进行了测试,我的Splash活动的实现非常简单,没有任何东西可以延迟onCreate(): 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_启动器);
waitTask=新的waitTask();
waitTask.execute();
}
私有类WaitTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
时间单位。毫秒。睡眠(飞溅持续时间);
}捕捉(中断异常e){
//无所事事
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
意向意向=新意向(LauncherActivity.this、MainActivity.class);
星触觉(意向);
完成();
}
}
我注意到,几乎所有的应用程序都会在第一个屏幕上延迟显示,然后显示这个白色屏幕,但在一些应用程序中,这个屏幕又回来了。有什么方法可以控制它吗?您可以定义活动的样式并在清单中进行设置。这样,当活动运行时,它将使用其预定义的样式(在该样式中,您将背景或windowBackground设置为红色),并且在0.3秒内,窗口仍将为红色 定义颜色:
<resources>
<color name="background">#FF0000</color>
</resources>
#FF0000
然后定义样式:
<resources>
<style name="MyLaunchActivityTheme" parent="@android:style/Theme.Light">
<item name="android:windowBackground">@color/background</item>
</style>
</resources>
@颜色/背景
然后在清单中设置活动的样式:
<activity
android:name=".MyActivity"
android:theme="@style/MyLaunchActivityTheme"/>
ans Sam提供的是完美的,但为了扩展它-让您希望自定义的启动屏幕图像始终可见,如果我们对窗口应用颜色,则会从颜色切换到实际的斜杠屏幕。我提议的ans不需要setContentView,只通过主题管理启动屏幕 我们不能用java设置主题,因为在我的例子中,控件很晚才开始创建我的splash活动。直到那时,黑屏仍然可见 这给了我一个线索,那个就是必须从我们在清单中指定的主题来管理窗口 这是我的清单:
<activity
android:name=".main.activities.SplashScreen"
android:theme="@style/Splash"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
现在我创建的主题如下:
<style name="Splash" parent="@style/Theme.AppCompat.Light">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">@color/green_09</item>
<item name="colorPrimary">@color/green_09</item>
<item name="windowActionBar">false</item>
</style>
@可牵引/飞溅
真的
真的
@颜色/绿色\u 09
@颜色/绿色\u 09
假的
Splash在包含位图资源的可绘制资源中,我必须稍微调整以使其看起来完美,而不是拉伸和居中:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:gravity="fill"
android:src="@drawable/splash_screen" />
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:gravity="fill"
android:src="@drawable/splash_screen" />