将activity_main.xml作为启动画面

时间:2016-05-10 10:55:54

标签: android xml

我是android的新手。 如果我想制作我当前的视图,我的应用程序作为启动画面5秒钟该怎么办? 是否有可能?

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background">

<ImageView
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="150dp"


    android:layout_centerInParent="true"
    android:contentDescription="TODO"
    android:src="@mipmap/app_icon_app_store"/>

<TextView
    android:id="@+id/firstLine"
    android:layout_width="300dp"
    android:layout_height="60dp"
    android:text="EXPRESSIONS"
    android:layout_centerHorizontal="true"
    android:textColor="#FFFFFF"
    android:textSize="50sp"
    android:layout_below="@+id/icon"
    android:gravity="center" />

    </RelativeLayout>

5 个答案:

答案 0 :(得分:1)

创建启动活动,您可以使用setContentView(R.layout.activity_main);

设置上面的布局
public class SplashActivity extends AppCompatActivity {


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

    setContentView(R.layout.activity_main);

    // this is count down timer for 5 seconds
    // 5000 milliseconds value is for 5 seconds display, 1000 milliseconds value is clock tick interval 
    new CountDownTimer(5000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
           // Launch whatever activity screen you want to display when done with count down timer (i.e. 5 seconds in your case)

            Intent intent = new Intent(SplashActivity.this,
                    YouNextActivity.class);//
            startActivity(intent);
            finish();

        }
    }.start();

}

}

答案 1 :(得分:0)

你可以这样做......

public class SplashScreenActivity extends Activity {
    // Splash screen timer
    private static int SPLASH_TIME_OUT = 5000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /****** Create Thread that will sleep for 5 seconds *************/
        Thread background = new Thread() {
            public void run() {
                try {
                    // Thread will sleep for 5 seconds
                    sleep(SPLASH_TIME_OUT);
                       Intent i = new Intent(MainActivity.this, AnotherActivity.class);
                        startActivity(i);
                        //Remove activity
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        };
        // start thread
        background.start();
    }
}

希望这会对你有所帮助。

答案 2 :(得分:0)

public class SplashActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGTH = 5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

答案 3 :(得分:0)

这是SplashActivity您只需在setContentView()

中设置当前视图
public class SplashScreen extends AppCompatActivity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    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(SplashScreen.this, YourActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}

答案 4 :(得分:0)

你需要让这个活动睡几秒钟 并在以后调用启动活动功能:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread splashTimer = new Thread() {
            public void run() {
                try {
                    sleep(3000);

                } catch (Exception e) {
                    Log.e("error", e.toString());
                } finally {
                    startMain();
                }
            }
        };
        splashTimer.start();
    }


    public void startMain(){
        Intent iMain = new Intent(this, MainActivity.class);
        startActivity(iMain);
        finish();
    }

startActivity()之后,如果按下后退按钮,则需要调用finish()以阻止此启动活动。