启动时空白页面

4
在启动应用程序时,在获得我的闪屏界面之前,我会看到一个空白页面,持续时间很短(<1秒)。我甚至尝试了几乎空白的闪屏活动,但仍然会出现这个问题!出了什么问题吗?
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity android:name=".SplashActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

SplashActivity.java

public class SplashActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.splash);
}

splash.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash"
        android:scaleType="centerCrop"/>

</LinearLayout>

编辑

我尝试使用默认项目,但问题仍未解决。求助!

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blankpage.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.blankpage.test.SplashActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

SplashActivity

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

splash.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".SplashActivity" 
    android:background="#654456">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

1
你尝试过在清单文件中加入android:theme="@android:style/Theme.NoTitleBar.Fullscreen"而不是在活动中使用this.requestWindowFeature(Window.FEATURE_NO_TITLE);吗?这个问题曾经困扰我的应用。 - MysticMagicϡ
@Anamika 是的,同样的问题。 - jul
还是显示空白屏幕? - MysticMagicϡ
是的...那是我唯一的问题。 - jul
我觉得我应该把这个链接留在这里:http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ - span
显示剩余4条评论
4个回答

3

以下是我所做的。

==> 经过大量搜索,我发现延迟是由Manifest文件中的“Theme”方面引起的。

因此,我根据网络上一些非常有用的示例创建了自己的样式(来源我真的忘记了)。

我建议你将以下内容添加到你的res/values/styles.xml中:

<resources>
    <style>
        ------- 
    </style> 

    <style name="SplashScreen">
        <item name="android:padding">0dp</item>
        <item name="android:windowBackground">@android:color/black</item>
        <item name="android:windowFrame">@null</item>
    </style>
</resources>

在你的 Manifest 文件中,只需将其作为 style 传递给你的 Activity 标签:

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sai.splashactivity.MainActivity"
        android:theme="@style/SplashScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

也许这会对您有所帮助。

我很高兴,它也为你起作用了! - sai

1

创建视图需要时间。而且,如果你只想显示图像,你的代码应该像这样:

   <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash"
        android:scaleType="centerCrop"/>

1)由于只有一个小部件,您不需要ViewGroup。

2)确保您设置为背景的图像大小不要太大。提供适当的图像。它将加载得更快。因为如果您的背景图像大小为2MB,则处理器需要时间在屏幕上绘制。


我甚至尝试了不使用“ImageView”。但我仍然得到了空白页面。 - jul
移除scaleType,它会在创建背景时执行一些图像缩放代码。只需提供适当大小和分辨率的图像,这样才能最小化加载时间。 - Hardik Trivedi
此外,您可以在清单文件中的Activity标签中使用android:theme="@android:style/Theme.Light"来避免黑色颜色。 - Hardik Trivedi

1
尝试这段代码。

splash.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash">
    </RelativeLayout>

0
我已经添加了这个:
<item name="android:windowDisablePreview">true</item>

将其返回到styles.xml中的自定义样式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接