您的位置:首页 > 移动开发 > Android开发

Android应用启动前 白屏或者黑屏原因?

2016-03-01 17:02 471 查看
在使用某些APP时会发现 点击应用稍有延迟显示黑屏 或者白屏 然后加载数据进入到应用界面?

那么为什么会出现短暂的黑屏或者白屏呢?什么时候出现黑屏 什么时候出现白屏呢?

延迟原因:因为再启动应用时 可能由于网络不稳定或者加载数据过多导致应用启动后 界面还没有显示 此时我们

看到的就是window的背景颜色。

黑屏或白屏原因:已经知道我们看到的是window的背景颜色了,那么为什么会有黑色和白色的区别呢?

这是因为继承主题的原因。

parent="Theme.AppCompat.NoActionBar"黑屏

parent="Theme.AppCompat.Light.NoActionBar" 白屏

可以通过设置windowbackground改变背景

例子:

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pv.mytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <strong>设置这里影响全部的activity</strong>
<activity android:name=".MainActivity"
android:theme="@style/MainTheme"><strong>设置mainactivity 窗口背景 主题只影响启动的activity</strong>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="MessengerActivity"></activity>
</application>
</manifest>


MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Thread.sleep(3000);/*延迟3秒 方便观察效果*/
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent intent = new Intent(this,MessengerActivity.class);
startActivity(intent);
}
}


styles.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MainTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- <item name="android:windowBackground">@color/colorAccent</item>颜色变成红色-->
</style>
</resources>


activity_main.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/white" <strong>设置背景颜色</strong>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pv.mytest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:onClick="onClick"
android:layout_centerHorizontal="true"
android:visibility="visible"
android:text="button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: