Правильный Splashscreen просто черный - PullRequest
0 голосов
/ 28 июня 2018

Итак, я видел много учебных пособий, показывающих «правильный способ» реализации SplashScreen для Android с использованием следующего кода:

<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>

^ drawable / splash_background.xml

<resources>

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

<!-- Splash Screen theme. -->

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_background</item>
</style>

^ values ​​/ styles.xml

  package com.rws.jsonclassifier;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override

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

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}

^ SplashActivity.java

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rws.jsonclassifier">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <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>

    <activity android:name=".MainActivity"></activity>
</application>

^ AndroidManifest.xml

Я использую код и следую инструкциям, как они показаны в руководствах, но по какой-то причине вместо синего фона со значком приложения я получаю полный черный экран. Есть идеи?

Ответы [ 3 ]

0 голосов
/ 28 июня 2018

Используйте обработчик внутри onCreate и попробуйте добавить новое представление макета splash.xml для активности всплеска:

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

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
       }
       }, 2000);
    }

вы можете добавить свой собственный дизайн в splash.xml layout

Если вы все еще хотите использовать свой способ кодирования, то я думаю, что ошибка в drawable / splash_background.xml

Добавьте ic_launcher в вашу папку для рисования и затем измените

<bitmap
    android:gravity="center"
    android:src="@mipmap/ic_launcher"/>

до

<bitmap
android:gravity="center"
android:src="@drawable/ic_launcher"/>
0 голосов
/ 28 июня 2018

После

super.onCreate(savedInstanceState);

добавить

setContentView(R.layout.activity_splash);

также добавьте обработчик, как сказал @Nabil

0 голосов
/ 28 июня 2018

Я думаю, это происходит потому, что вы не используете Handler для перехода к следующему Activity. Я реализовал это в одном из моих приложений.

AndroidManifest.xml

<activity
     android:name=".view.LaunchActivity"
     android:label="@string/app_name"
     android:launchMode="singleTask"
     android:theme="@style/AppTheme.Launch">
     <intent-filter>
          <action android:name="android.intent.action.MAIN" />

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

styles.xml

<style name="AppTheme.Launch" parent="AppTheme.NoActionBar">
        <item name="android:windowBackground">@drawable/activity_launch</item>
</style>

activity_launch.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/product_logo_windesheim" />
    </item>
</layer-list>

Затем заставьте LaunchActivity выдвинуться Activity.

 @Override
 public void onCreate(Bundle bundle) {
     super.onCreate(bundle);
     new Handler().post(new Runnable() {
         @Override
         public void run() {
            Intent intent = new Intent(LaunchActivity.this, ScheduleActivity.class);
            startActivity(intent);
            finish();
         }
     });
 }

См. Полный источник здесь:

https://github.com/gi097/MyWindesheim/

...