【问题标题】:Android display Splash-Screen while loading but getting black screen insteadAndroid在加载时显示Splash-Screen但改为黑屏
【发布时间】:2016-12-16 08:21:32
【问题描述】:

我正在尝试来自这个问题的建议 #2: Android display Splash-Screen while loading

这是使用带有背景可绘制对象的主题,然后从 Splash.java 加载 MainActivity,Splash.java 在 MainActivity 处理和加载时使用所述主题显示图像。

我黑屏几秒钟,然后在第一次加载时出现主屏幕。

我错过了什么吗?或者这是否正常工作?如果它工作正常,如果可能,我如何让图像显示而不是黑屏?

@drawable/background_splash

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

    <!-- Fill the background with a solid color -->
    <item
        android:drawable="@color/colorPrimary"/>
    <item>

        <!-- Place your bitmap in the center -->
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

@样式

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

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<!-- Splash Screen theme -->
<style name="SplashTheme">
    <item name="android:background">@drawable/background_splash</item>
    <item name="android:windowAnimationStyle">@null</item>
</style>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.epicdecals.openslider">

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

<application
    android:allowBackup="true"
    android:icon="@drawable/open_slider_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

</application>

</manifest>

Splash.java

public class Splash extends AppCompatActivity {

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

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

【问题讨论】:

  • 初始屏幕的 xml 在哪里。
  • 你正在使用Splash extends Activity,你必须使用这个Splash extends AppCompatActivity
  • @Champandroid 根据文章没有故意的。
  • @Ironman 完成但没有改变任何东西。

标签: android themes drawable splash-screen


【解决方案1】:

我认为您的 Flash 屏幕未加载,因为您没有设置启动屏幕的任何时间。
试试吹码:

public class Splash extends Activity {

private CountDownTimer timer = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    timer = new CountDownTimer(2000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
               Intent intent = new Intent(this, MainActivity.class);
               startActivity(intent);
               finish();
        }
    };
}

@Override
protected void onResume() {
    super.onResume();
    timer.start();
}

@Override
protected void onPause() {
    super.onPause();
    timer.cancel();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    timer = null;
}

【讨论】:

  • 无法解析构造函数'intent' 我真的只希望它在启动时显示(在没有屏幕可见的情况下做某事)我的印象是它会显示而不是我得到的黑屏.
【解决方案2】:

// 这对我有用。 // 这是 Splash.java

public class Splash extends ActionBarActivity
 {



    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

            Thread th = new Thread() {
            public void run() {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                  // write your activity which you want to open

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

                }
            }
        };
        th.start();
    }

}

// 这是我的 Splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bk_client"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_centerInParent="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_centerInParent="true"/>

      </LinearLayout>

</RelativeLayout>

【讨论】:

    【解决方案3】:

    我必须在样式主题中添加一个父对象才能使其正常工作。感谢您在 cmets Redman 中提供链接 ^^^^。

    前:

    <style name="SplashTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@drawable/background_splash</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      相关资源
      最近更新 更多