MainActivity.java
/** * @author Administrator * @param 說明可以用代碼控制 布局,不用xml * */public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //建立一個線性布局管理器 new 1個 LinearLayout LinearLayout layout = new LinearLayout(this); //設定該Activity顯示layout super.setContentView(layout); layout.setOrientation(LinearLayout.VERTICAL); //TextView final TextView tv = new TextView(this); //按鈕 Button b = new Button(this); b.setText(R.string.ok); b.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT ,ViewGroup.LayoutParams.WRAP_CONTENT)); //向Layout容器中添加TextView layout.addView(tv); //向Layout容器中添加按鈕 layout.addView(b); //為按鈕綁定一個事件監聽器 b.setOnClickListener(new OnClickListener(){ public void onClick(View v){ tv.setText("Hello , Android," + new java.util.Date()); } }); }}
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">Test69</string><string name= "ok">我把 show 改成 tv了, 這裡局部變數調用 使用final **局部變數</string></resources>
從上面的程式的粗體字代碼可以看出,該程式中所用到的UI組件都是通過new關鍵字建立出來的,然後程式使用LinearLayout容器來“盛裝”這些UI組件,這樣就組成一T圖形化使用者介面。運行結果
在模擬器中運行上面的程式將可以看到2.4所示介面。 從上面的程式碼中不難看出,完全在代碼中控制UI介面不僅不利於高層次的解耦合.而且由於通過
new關鍵來建立
UI組件,需要調用方法來設定UI組件的行為.因此代碼也顯得十分臃腫;相反,如果通過
XML檔案來控制UI介面,開發人員只要在XML布局檔案中使用標籤即可建立UI組件,而且只要配置簡單的屬性即可控制UI組件的行為,因此要簡單得多。 提示:一··一··一··一··一··一··一··一··一··一··一··一··一 雖然Android應用完全允許開發人員像開發Swing應用一樣在代碼中控制UI. 介面,但這種方式不僅編程煩瑣,而且不利於
高層次的解藕,因此不推薦開發人員 使用這種方式.一
> > 2.1.4使用XML布局檔案和Java代碼混合控制UI介面 當混合使用XML布局檔案和代碼來控制U1介面時,習慣上把變化小、行為比較固定的組件放在XML布局檔案中管理,而那些
變化較多、行為控制比較複雜的組件則交給Java代碼來管理。
執行個體:簡單圖片瀏覽器 例如下面的應用,我們先在布局檔案中定義一個簡單的線性版面配置容器,該布局檔案的代碼如下。程式清單:codes\02\2.1\Mix View\res\Iayout\main.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" android:id="@+id/root" >
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity { //定義訪問圖片的數組 int[] images = new int[]{ R.drawable.mm0, R.drawable.mm01, R.drawable.mm02, R.drawable.mm03, R.drawable.mm04, R.drawable.mm05, R.drawable.mm06, R.drawable.mm07, R.drawable.mm08, }; int currentImg = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //擷取LinearLayout版面配置容器 LinearLayout mian = (LinearLayout)findViewById(R.id.root); //程式建立ImageView組件 final ImageView image = new ImageView(this); //將ImageView 組件添加到LinearLayout版面配置容器中 mian.addView(image); //初始化顯示第一張圖片 image.setImageResource(images[0]); image.setOnClickListener(new OnClickListener(){ public void onClick(View v){ if(currentImg >= 4){ currentImg = -1; } //改變ImageView裡顯示的圖片 image.setImageResource(images[++currentImg]); } }); }}
運行如下
上面的程式中第一行粗體字代碼擷取了該Activity所顯示的LinearLayou“線性布局管理器).
第2和第3行粗體字代碼用於建立一個ImageView,並將該ImageView添加到—其中LinearLayout布局管理器通過XML布局檔案管理,而ImageView組件則由Java代碼管理。 除此之外,上面的程式還為ImageView組件添加了一個單擊事件。當使用者單擊該組件時,ImageView顯示下一張圖片。運行上面的程式可以看到2.5所示介面。