Android欢迎界面,一个Activity搞定

晚上现在很多欢迎界面,我最近有个要做欢迎界面的事,然后上网上一搜,有不少,但是基本都是延时的activity跳转,我记得我多年前学习Android时候,似乎一个一个activity就行。但是,我发现网上好像找不到,如果哪位大神有好的网站中有说到这个麻烦告知,我下面要讲的这个,简单点做至少需要两个layout文件,一个activity文件。废话不多说,这个是我自己想出来的。由于对于Android本身机制不清楚,大神勿喷,谢谢。

代码。MainActivity.java

package com.example.my.myapplication;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

    private void welcome() {
        Handler handler = new Handler();
        //当计时结束,跳转至主界面
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                Intent intent = new Intent(MainActivity.this, MainActivity.class);
//                startActivity(intent);
//                MainActivity.this.finish();
                setContentView(R.layout.activity_main);
            }
        }, 3000);
    }
}

activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

layout.xml代码

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

没错,就是这么简单就搞定了这个欢迎界面。

猜你喜欢

转载自blog.csdn.net/qq_20081893/article/details/81127521
今日推荐