即使布局非常简单和轻便,SetContentView()也需要时间

时间:2013-11-12 09:34:32

标签: android android-layout

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

xml layout:

<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

1 个答案:

答案 0 :(得分:1)

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

  1. 删除背景绘图并进行测试。
  2. 删除每个第3层相对布局并进行测试。
  3. p / s:我认为你实际上可以减少一层相对布局。第二层相对布局可以与第一层合并。