不会出现在xml(没有活动)中定义的SplashScreen方法

时间:2017-12-06 08:49:57

标签: android splash-screen

我已经定义了很多其他没有外部活动的启动画面,但它对我不起作用,我不明白为什么。

图片有限制吗?还尝试了许多其他图像 这是我的splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/splash_logo" />
    </item>
</layer-list>

这里android studio中的splash.xml文件预览看起来很完美。 比我创造的样式

<style name="AppTheme.Launcher">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

并将其分配给清单

中的活动主题
 <activity
        android:theme="@style/AppTheme.Launcher"
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

这是我的活动

override fun onCreate(savedInstanceState: Bundle?) {
    setTheme(R.style.AppTheme)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}

当加载活动时,显示空白屏幕而不是图像,可能是一秒钟。我搜索了这种方法,似乎和其他人一样。不明白什么是错的

修改 我已经测试了其他设备OS 5.0并且它可以工作,但是对于6.0不能

2 个答案:

答案 0 :(得分:2)

修改

从您的活动中删除

setTheme(R.style.AppTheme)

试试这个

<强> Style.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">

        <item name="android:windowBackground">@drawable/splash_background</item>

</style>

<强> splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary" />

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher" />
    </item>
</layer-list>

<强> SplashActivity

public class SplashActivity extends AppCompatActivity {

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

        startActivity(new Intent(SplashActivity.this, MainActivity.class));

        // close splash activity

        finish();
    }
}

<强>清单

<activity android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

答案 1 :(得分:0)

这应该是评论。但我不能发表评论。我遇到了同样的问题。请检查您的图像大小。尝试压缩图像并再试一次它可能对您有所帮助。请告诉我。