启动画面 Android

Splash screen Android

我是初学者,我想显示启动画面,但我的老板想使用 Fragment。我不知道如何在我的 MainActivity 中实现它。我知道我必须使用具有时间和堆栈布局的处理程序,但我还没有找到如何。 谢谢

您不需要处理程序来执行此操作,请按照以下步骤操作:

1 - 为启动画面创建一个 activity,在它准备好后启动您的 mainActivity:

public class SplashActivity extends AppCompatActivity {

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

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

2 - 在 pap 甚至可以展开布局之前,启动画面必须准备就绪,因此我们创建一个 xml 文件并将其定义为 window 的背景,这里是一个灰色背景的 xml 文件,中间有一个图像(称为 background_splash.xml):

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

    <item
        android:drawable="@color/gray"/>

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

</layer-list>

3 - 现在进入您的 styles.xml 并使用我们刚刚创建的图片作为背景设置新样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

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

</resources>

4 - 最后进入清单文件,将 SplashActivity 定义为启动器 activity,并在其上设置新样式:

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

如果您想在 MainActivity 中添加启动画面作为片段

  1. 向您的 MainActivity 添加一个 FrameLayout,在 FrameLayout 内,为您的视图创建一个布局,并插入一个包含您的 SplashScreen 显示的 Fragment(确保您的 Fragment 位于 MainActivity 的底部,以便其显示在上方你的布局)

例子

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent">

         //Your UI goes here        

   </LinearLayout>


  <Fragment
      Your Fragment with Splash Screen Goes here 
   />
</FrameLayout>



  1. 在您的 FragmentActivity 中执行此操作

     new Handler()..postDelayed(new Runnable() {
        @Override
        public void run() { 
    
           Intent intent = getActivity().getIntent();
           getActivity().finish();
           startActivity(intent);
    
      }
    },2000);
    

在android中非常简单,我们只是使用处理程序概念来实现启动画面

在您的 SplashScreenActivity java 文件中粘贴此代码。

在您的 SplashScreenActivity xml 文件中使用 imageview 放置任何图片。

public void LoadScreen() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {                 
                Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
                startActivity(i);
            }
        }, 2000); // you can increase or decrease the timelimit of your screen
    }

在科特林中#Kotlin #Splash #LuanchScreen

 private fun waitForWhile() {
    Handler(Looper.getMainLooper()).postDelayed({
        callNextActivity()
    }, 3000)

}

private fun callNextActivity() {
    val intent = Intent(this,SignInActivity :: class.java)
    startActivity(intent)
    finish()
}