启动画面Android

时间:2016-06-16 13:24:06

标签: android android-fragments splash-screen

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

4 个答案:

答案 0 :(得分:2)

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

1 - 为启动屏幕创建活动,以便在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文件并将其定义为窗口的背景,这里是一个灰色的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
    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>

答案 1 :(得分:0)

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

  1. 将FrameLayout添加到您的MainActivity,在FrameLayout内,为您的视图制作一个布局,并插入一个包含SplashScreen显示的片段(确保您的片段位于MainActivity的底部,以便它显示在您的布局上)
  2. 实施例

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

答案 2 :(得分:0)

它在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
    }

答案 3 :(得分:0)

在 Kotlin #Kotlin #Splash #LunchScreen

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

}

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