您好我是Android应用程序开发的新手,我想在我的应用程序中添加一个启动画面,就像iPhone应用程序中出现的那样,仅在我的应用程序加载时持续。
在大多数地方,我发现代码会为启动添加一个单独的屏幕,并将其显示为1-5秒。
在我看来,这不是启动画面的实际概念。所以任何人都可以告诉我如何在Android应用程序中添加启动画面。
答案 0 :(得分:1)
创建SplashScreen,扩展Activity,然后在其中编写此代码块
警告:MainActivity是您的基本活动,在您创建新项目时是默认活动
public class SplashScreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 2000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
finish();
}
}
};
splashThread.start();
}
}
顺便说一句,如果您是Android开发的新手,请在实施SplashScreen之前了解Thread
主题
答案 1 :(得分:0)
在您的活动中,在加载数据时,您可以添加一个片段,以显示您想要的任何进度。一旦活动完成加载,您就可以删除该片段。
其他选项,使用https://github.com/marvinlabs/android-progress-panel之类的小部件,可以切换视图。
答案 2 :(得分:0)
对于启动画面,只需使用简单的图像创建布局(如果您不需要想象动画)
splash_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/image" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:textSize="12dp"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Some Text Here" />
</RelativeLayout>
在SplashScreenActivity.java类中,使用以下代码替换onCreate方法。
//Time for which you want to show splash screen
private static int SPLASH_SCREEN_TIME_OUT = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_SCREEN_TIME_OUT);
}
答案 3 :(得分:0)
试试这个
package com.sunil.assignment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
public class Splash extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
final Fragment spl=new Fragment();
FragmentTransaction ft= getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, spl);
ft.commit();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//Intent intent=new Intent(Splash.this,MainActivity1.class);
// Splash.this.startActivity(intent);
//Splash.this.finish();
findViewById(R.id.fragment1).setVisibility(View.GONE);
Home h=new Home();
FragmentTransaction ft=getFragmentManager().beginTransaction();
//ft.hide(spl);
ft.replace(R.id.rel1, h);
ft.commit();
}
}, 2000);
}
}
splashh.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rel1" >
<fragment
android:id="@+id/fragment1"
android:name="com.sunil.assignment.Spl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
答案 4 :(得分:0)
试试这个, 并在 AndroidManifest.xml
中将SplashScreenActivity作为启动器活动 public class SplashScreenActivity extends Activity {
private static int SPLASH_TIME_OUT = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
答案 5 :(得分:0)
显示启动画面并在后台加载数据。当您加载了所需的所有内容后,请通知您的活动。当您的活动收到该通知时,只需启动您的下一个(主要)活动。
答案 6 :(得分:0)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// code u want to run after 5 second
//startactivity(new Intent(Splash.this,MainScreen.class))
//finish();
}
},5000);