人說天下大勢分久必合合久必分,寫程式也是這樣的,永遠有計畫趕不上變化的時候,

這時候擁有幹你娘超群智慧的我們就必須做長遠的規劃。

 

假設今天要做一個頁面,上頭的元件是會動態變化的,我們就不能把元件寫死在主頁面上,

因此主頁面的 resource 檔可以寫成如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout >
</LinearLayout>

 

可以發現在 Linear layout 中又包了一個 Relative layout,可以把它視為一個 container。

程式中的寫法如下:

RelativeLayout container = (RelativeLayout)rootView.findViewById(R.id.container);
RelativeLayout layout = (RelativeLayout) View.inflate(getActivity(), R.layout.layout_report_issue, null);
container.addView(layout);

 

帥!直接動態把所需要的 layout 加到 container 中,需要用到動態 layout 中的元件時以此採取以下寫法:

mSpinAppList = (Spinner) layout.findViewById(R.id.spAppList);
mEdtReporter = (EditText) layout.findViewById(R.id.edtRepoter);
mEdtDescription = (EditText) layout.findViewById(R.id.edtDescription);

 

擒猿之路,不由分說。

arrow
arrow
    全站熱搜

    擒猿小舖 發表在 痞客邦 留言(0) 人氣()