android开发 解决启动页空白或黑屏问题

时间:2023-03-09 08:57:53
android开发 解决启动页空白或黑屏问题

遇到的情况:

app启动时进入启动页时出现白屏页,然后大概一秒之后就出现了背景图片。

原因:app启动时加载的是windows背景,之后再加载布局文件的,所以开始的黑屏/白屏就是windows的背景颜色,因此我们只要在启动页设置windows背景颜色就好了,那么在哪里设置呢?  就是theme里面。

解决办法:参考:【Android Drawable 那些不为人知的高效用法

Android 启动APP时黑屏白屏的三个解决方案

首先看之前的布局文件xml写法

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/bg_app_welcome_1"/>
<LinearLayout
android:id="@+id/v_prepare_tips"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#007ed2"
android:text="首次安装正在准备数据"/>
<ProgressBar
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"/>
</LinearLayout> </RelativeLayout>

=========================================================

优化之后的:

<!-- 只是将根元素的背景去掉了 -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/v_prepare_tips"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#007ed2"
android:text="首次安装正在准备数据"/>
<ProgressBar
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</RelativeLayout>

设置style.xml

    <style  name="AppStartingBg"  parent="@android:style/Theme.Holo.Light.NoActionBar" >
<item name="android:windowBackground">@drawable/bg_appstarting</item>
</style>
bg_appstarting.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<bitmap android:src="@drawable/bg_app_welcome_1" android:gravity="fill" />
</item>
</layer-list>

再在manifest.xml文件中添加该ancivity的theme

<activity
android:name="com.client.activity.AppStartingActivity"
android:icon="@drawable/ic_logo_2"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppStartingBg" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

大功告成!