在主活动的setContentView之前显示图像几秒钟

时间:2013-10-10 12:02:07

标签: android layout

我想在设置主要活动的最终布局之前显示“引导徽标”或图像。实际上我这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.authors);
    mContext = this;
    showAuthors();

showAuthors运行它:

private void showAuthors()
{
    setContentView(R.layout.authors);
    Thread logoTimer = new Thread() {
        public void run() {
            try {
                sleep(3000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {

            }
        }
    };

    logoTimer.start();
    try {
        logoTimer.join();
        setContentView(R.layout.activity_main);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

文件authors.xml(R.layout.authors)与activity_main相同,但是干净,只包含一对带有我姓名和电子邮件的字符串:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@raw/call2"
android:src="@raw/call2"
android:scaleType = "centerCrop"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/authorsTV"
    android:textColor="#FF6600"
    android:textSize="16.5sp"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="33dp"
    android:text="@string/serviceStatus" />

</RelativeLayout>

问题是:一切正常,但屏幕全是白色的空布局 哪里我做错了?如果考虑回复我,谢谢大家!

2 个答案:

答案 0 :(得分:1)

首先,您有两种显示启动画面的方式,

1.with活动

public class SplashActivity extends Activity implements Runnable
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        ...
        setContentView(R.layout.act_spalsh);
        hideSplashActivity();
    }
    private void hideSplashActivity()
    {
        new Handler().postDelayed(this, 3000);
    }
    @Override //from runnable
    public void run()
    {
        startActivity(new Intent(this, MainActivity .class));
        finish();

    }

2.with dialog

public class MainActivity extends Activity implements Runnable
{
    Dialog splashDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        ...
        splashDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        splashDialog.setContentView(R.layout.dlg_splash);
        splashDialog.show();
        hideSplashDialog();
        setContentView(R.layout.act_main);
    }
    private void hideSplashDialog()
    {
        new Handler().postDelayed(this, 3000);
    }
    @Override //from runnable
    public void run()
    {
        splashDialog.dismiss();
        splashDialog = null;
    }

除非你遇到麻烦,否则我更喜欢使用对话框

答案 1 :(得分:1)

你不应该从主线程调用thread.join,因为你可以得到ANR崩溃。

以下是对现有代码进行最少更改的方法。在logoTimer.start()之后删除所有内容,并在sleep(3000)之后立即将其放入try块中:

runOnUiThread(new Runnable(){
    public void run(){
        setContentView(R.layout.activity_main);
    }
});

但重写像这样的整个方法会更清晰:

private void showAuthors()
{
    setContentView(R.layout.authors);
    new Handler().postDelayed( new Runnable(){
        public void run(){
            setContentView(R.layout.activity_main);
        }
    }, 3000);
}