1

setContentView()的加载大约需要 3 秒,即使它的布局非常简单,没有任何自定义视图。

xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/homeScreenLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/mainview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_home"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/homeButtonsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <Button
                android:id="@+id/btnHomeSearch"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:id="@+id/tvHomeSearch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnHomeSearch"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <Button
                android:id="@+id/btnHomeClip"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:id="@+id/tvHomeClip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnHomeClip"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dp" >

            <Button
                android:id="@+id/btnSeeAll"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnSeeAll"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>


下面是 onCreate() 的一部分:

Log.i("PN","Main onCreate() start");
setContentView(R.layout.main_flipper);
Log.i("PN","Main onCreate() aftr setContentView");

ViewFlipper只有 2 个布局。第二种布局也是轻量级的。


对应日志:

11-12 09:23:01.622: I/PN(12081): Main onCreate() start
11-12 09:23:04.324: I/PN(12081): Main onCreate() aftr setContentView

请注意,这setContentView()大约需要 3 秒,这是相当高的。

知道为什么会这样吗?

任何帮助表示赞赏。

编辑

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        Log.i("PN","Main onCreate() start");
        // Set the design screen for the code
        // Activate full screen for this flow
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main_flipper);
        Log.i("PN","Main onCreate() aftr setContentView");

        Bundle b = getIntent().getExtras();
        Log.i("PN","Main onCreate() aftr getExtras");
        int viewNo = 0;
        if (b != null) {
            viewNo = b.getInt("View");
        }

        initVariables();

        setOnClickListeners();
        initSearchView();
        setTextWatcher();
        vf.setDisplayedChild(viewNo);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

更新日志:

11-12 09:42:12.532: I/PN(14342): Main onCreate() start = 1384249332537
11-12 09:42:15.122: I/PN(14342): Main onCreate() aftr setContentView = 1384249335125
11-12 09:42:15.142: I/PN(14342): Main onCreate() aftr getExtras = 1384249335142
11-12 09:42:15.142: I/PN(14342): Main onCreate() b4 init = 1384249335142
11-12 09:42:15.392: I/PN(14342): Main onCreate() End = 1384249335396

正如我们所看到的,主要延迟发生在设置setContentView()

编辑 (2)

更新日志:

11-12 09:48:11.342: I/PN(15081): Main onCreate() start = 1384249691345
11-12 09:48:11.372: I/PN(15081): Main onCreate() aftr FLAG_FULLSCREEN = 1384249691372
11-12 09:48:13.961: I/PN(15081): Main onCreate() aftr setContentView = 1384249693970
4

1 回答 1

1

您需要逐项卸载以查看原因。我的建议:

  1. 删除背景绘图并测试。
  2. 删除每个第 3 层相关布局并进行测试。

p/s:我认为您实际上可以减少一层相对布局。第二层相对布局可以与第一层合并。

于 2013-11-12T09:41:09.830 回答