【问题标题】:Android: Splash Screen Implementation Not WorkingAndroid:启动画面实现不起作用
【发布时间】:2017-01-14 13:32:41
【问题描述】:

我正在尝试在 MainActivity 之前实现启动画面,但无法使其正常工作。我按照此处的说明进行操作:https://www.bignerdranch.com/blog/splash-screens-the-right-way/。目前除了黑色延迟屏幕什么都没有出现。请告诉我哪里出错了。谢谢。

Splash.java

public class Splash extends AppCompatActivity {

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

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

background_splash.xml

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:drawable="@color/colorAccent"/>
        <item>
            <bitmap
                android:gravity="center"
                android:src="@drawable/splash"/>
        </item>

styles.xml

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

清单

<activity android:name=".Splash"
            android:theme="@style/SplashTheme">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>

【问题讨论】:

  • 在调用 Intent 意图之前 .. 给它一些时间来显示像 2/3 秒这样的飞溅
  • 是的,我会试试的。
  • new Handler().postDelayed(new Runnable() { @Override public void run() { // Your Intent } }, 2000);
  • 谢谢。我发现了问题。 background_splash.xml 根本没有显示。你知道为什么它可能没有显示吗?
  • 还要确保其他活动 WelcomeActivity 具有正确的主题,否则它将继承自您的主应用主题 android:theme="@style/SplashTheme"

标签: java android android-xml android-styles


【解决方案1】:

简单,卸载并再次安装应用程序,您将获得启动画面。 问题是由于在 Android Studio 中即时运行造成的

要进行测试,请在开始新活动之前延迟:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    startActivity();
  }
}, 100);

【讨论】:

  • 请延迟检查
  • 好的,我想我可能知道问题出在哪里。背景颜色显示不正确。
  • 这不是确切的答案,但我会接受它,因为它有助于诊断根源。
【解决方案2】:

super.onCreate(savedInstanceState);下方添加 setContentView(R.layout.layout_name.xml);

【讨论】:

  • 他没有要求 setContentView
猜你喜欢
  • 1970-01-01
  • 2022-12-20
  • 2014-11-19
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多