0

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

@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>

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

4

2 回答 2

1

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

1.有活动

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.带对话框

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

我更喜欢使用对话框,除非你有一些麻烦

于 2013-10-10T12:21:26.027 回答
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);
}
于 2013-10-10T12:27:02.267 回答