android 中 setContentView() 的前世今生

新建一個android項目時,  初始化activity時,總能看到到setContentView(R.layout.main);這行代碼,以前只知道這行代碼能把main佈局文件的內容佈局加載並顯示出來,但是加載的過程並不是十分清楚,今天經過查詢資料,對他的實現過程總結一下,自己學習的同時也希望能幫助有相同疑惑的同仁們。

android的編寫與java有着莫大的淵源,我們從java一路走來,習慣於任何對象都是new出來,android與java一脈相承,那麼在這裏面我們需要的對象可以自己new嗎?

回答是肯定的,下面的控件都是通過硬編碼new出所需的控件,

 LayoutParams params=new LayoutParams(0,LayoutParams.WRAP_CONTENT, 1.0f);//注意是那個佈局的佈局參數,如線性佈局
       
       TextView tvUserName=new TextView(this);
       tvUserName.setText("用戶名");
       EditText etUserName=new EditText(this);
       etUserName.setHint("請輸入用戶名");
       etUserName.setLayoutParams(params);//設置佈局參數
       LinearLayout row1=new LinearLayout(this);
       row1.addView(tvUserName);
       row1.addView(etUserName);
       
       TextView tvPassword=new TextView(this);
       tvPassword.setText("密	碼");
       EditText etPassword=new EditText(this);
       etPassword.setHint("請輸入密碼");
       LinearLayout row2=new LinearLayout(this);
       row2.addView(tvPassword);
       row2.addView(etPassword,params);
       
       
       Button btnLogin=new Button(this);
       btnLogin.setText("登陸");
       Button btnCancle=new Button(this);
       btnCancle.setText("取消");
       LinearLayout row3=new LinearLayout(this);
       row3.addView(btnLogin,params);
       row3.addView(btnCancle,params);
       
       LinearLayout root=new LinearLayout(this);
       root.setOrientation(LinearLayout.VERTICAL);
       root.addView(row1);
       root.addView(row2);
       root.addView(row3);
       this.setContentView(root);
我們並把多有的組件組成視圖,然後單擊運行,出現下面的界面:
這說明採用硬編碼的方式完全可以實現將佈局文件文件顯示出來,但是採用這種方式顯示,需要書寫大量的代碼,增加程序員的負擔,因此,android把佈局放到xml文件中,
作爲資源的一部分,通過LayoutInflater這個類將xml佈局文件加載並顯示成對應視圖。解析xml文件需要用到的方法有LayoutInflater的靜態方法from來獲取一個LayoutInflater實例inflater,再通過inflater的inflate方法返回一個視圖實例,最後通過setContentView的方法把這個視圖實例加載出來。實現的代碼如下:
對應的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用戶名" />

        <EditText
            android:id="@+id/etUserName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tvPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    碼" />

        <EditText
            android:id="@+id/etUserPass"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="登    錄" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="取    消" />
    </LinearLayout>

</LinearLayout>

activity中用到的相關代碼:


	LayoutInflater inflater=LayoutInflater.from(this);
      	LinearLayout root= (LinearLayout) inflater.inflate(R.layout.main, null);
       this.setContentView(root);
LayoutInflater這個類一般用在非activity中設置相應的視圖;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章