【问题标题】:Splash Screen not working in kitkat and lower api level in android启动画面在 kitkat 中不起作用并且在 android 中的 api 级别较低
【发布时间】:2017-05-01 10:05:49
【问题描述】:

我在我的 Android 应用程序中使用启动画面。它适用于棒棒糖和更高版本,但不适用于 kitkat 和更低版本。 问题出在 menifest.xml 中,但我无法修复它。

<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=".SplashScreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

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


SplashScreenActivity.java

公共类 SplashScreenActivity 扩展 Activity {

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

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

    StartAnimations();
    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 5 seconds
                sleep(3*1000);

                // After 5 seconds redirect to another intent
                Intent i=new Intent(getBaseContext(),MainActivity.class);
                startActivity(i);

                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    // start thread
    background.start();



}

private void StartAnimations() {

    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.logo);
    iv.clearAnimation();
    iv.startAnimation(anim);

}

}


在 res/anim/alpha.xml 中

alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="3000" />

【问题讨论】:

  • 显示代码,活动代码
  • 我认为 menifest.xml 文件中没有问题...你能发布 SplashScreenActivity java 代码吗?
  • 也分享你的java代码和xml。
  • 所有建议分享您的活动代码
  • 签出下面的java代码

标签: android splash-screen


【解决方案1】:

有我的 SplashScreen。它适用于 android 4.1 及更高版本。


SplashScreen.java

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen_layout);

    }

    @Override
    protected void onStart() {

        super.onStart();


        // load your stuff here ...


        Intent intent = new Intent(SplashScreen.this, YourHomeActivity.class);
        startActivity(intent);
        finish();

    }
}


res/layout/splashscreen_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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:fitsSystemWindows="false"
    tools:context=".SplashScreen"
    android:theme="@style/SplashTheme">

</android.support.design.widget.CoordinatorLayout>


添加你的 res/values/styles.xml

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


res/drawable/splashscreen_drawable.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="@drawable/your_icon" />
    </item>

</layer-list>


还有我的 AndroidManifest.xml

<application

        [...]

        >
        <activity
            android:name="com.your.package.app.SplashScreen"
            android:label="@string/app_name"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        [...]


它看着这个:

【讨论】:

    猜你喜欢
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多