編輯:Android 開發實例
關於 Android 常用佈局,利用 XML 檔實作的範例已經很多。那麼該如何用程式碼來實作呢?雖然純用程式碼佈局沒有太大必要、也不太被鼓勵,但我認為透過程式碼實作這些佈局,能更深入了解 SDK API,因此在此整理出來與大家分享。
先來看一下佈局所對應類別的 API 繼承圖:

Android 常用佈局的程式碼實作中,每個佈局都會對應相關的類別,這些類別都繼承自 android.view.ViewGroup。而 LinearLayout、RelativeLayout 則位於 android.widget 套件裡;另外,TableLayout 是繼承自 LinearLayout 的。
下面直接看程式碼。
// 利用代碼設置 線性布局 private void setLinearLayout(){ LinearLayout llayout = new LinearLayout(this); llayout.setOrientation(LinearLayout.VERTICAL); // 設置線性布局的排列方式 TextView textView = new TextView(this); textView.setText("代碼實現的線性布局"); textView.setTextColor(Color.RED); textView.setGravity(Gravity.CENTER); // 設置文本內容的對齊方式 LinearLayout.LayoutParams ll_lpara = new LinearLayout.LayoutParams(MP,WC); // ll_lpara.gravity = Gravity.CENTER_HORIZONTAL; // 設置控件在布局中的對齊方式 llayout.addView(textView,ll_lpara); Button btn = new Button(this); btn.setText("按鈕"); llayout.addView(btn,ll_lpara); // 按指定屬性添加控件 setContentView(llayout); }效果圖如下:

// 利用代碼設置 相對布局 private void setRelativeLayout(){ RelativeLayout rlayout = new RelativeLayout(this); rlayout.setPadding(10, 10, 10, 10); // 單位: pixels int textViewID = 100; TextView textView = new TextView(this); textView.setId(textViewID); textView.setText("請輸入:"); RelativeLayout.LayoutParams rl_lpara1 = new RelativeLayout.LayoutParams(MP, WC); rlayout.addView(textView, rl_lpara1); int editTextID = 200; EditText editText = new EditText(this); editText.setId(editTextID); editText.setBackgroundResource(android.R.drawable.editbox_background); // 設置背景 , 同android:backgroumd RelativeLayout.LayoutParams rl_lpara2 = new RelativeLayout.LayoutParams(MP, WC); rl_lpara2.addRule(RelativeLayout.BELOW,textViewID); // 設置相對屬性,需先指定相對控件的ID rlayout.addView(editText, rl_lpara2); int backBtnID = 300; Button backBtn = new Button(this); backBtn.setId(backBtnID); backBtn.setText("返回"); RelativeLayout.LayoutParams rl_lpara3 = new RelativeLayout.LayoutParams(WC, WC); rl_lpara3.addRule(RelativeLayout.BELOW, editTextID); rl_lpara3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 設置與父控件的相對屬性 rlayout.addView(backBtn, rl_lpara3); Button okBtn = new Button(this); okBtn.setText("確定"); RelativeLayout.LayoutParams rl_lpara4 = new RelativeLayout.LayoutParams(WC, WC); rl_lpara4.addRule(RelativeLayout.LEFT_OF, backBtnID); rl_lpara4.addRule(RelativeLayout.ALIGN_TOP,backBtnID); rlayout.addView(okBtn, rl_lpara4); setContentView(rlayout); }效果圖如下:

// 利用代碼設置 表格布局 private void setTableLayout(){ TableLayout tlayout = new TableLayout(this); tlayout.setColumnStretchable(2, true); // 拉長索引從0開始的第2列 TableLayout.LayoutParams tl_lpara = new TableLayout.LayoutParams(MP,WC); // 1. TableRow 不需要設置 layout_width, layout_height // 2. TableRow 中的控件不能設置 layout_span 屬性 TableRow tr1 = new TableRow(this); TextView textView0 = new TextView(this); textView0.setText("第0列"); tr1.addView(textView0); TextView textView1 = new TextView(this); textView1.setText("第1列"); tr1.addView(textView1); TextView textView2 = new TextView(this); textView2.setText("第2列"); textView2.setBackgroundColor(Color.CYAN); tr1.addView(textView2); tlayout.addView(tr1, tl_lpara); TableRow tr2 = new TableRow(this); Button btn0 = new Button(this); btn0.setText("按鈕0"); tr2.addView(btn0); Button btn1 = new Button(this); btn1.setText("按鈕1"); tr2.addView(btn1); Button btn2 = new Button(this); btn2.setText("按鈕2"); tr2.addView(btn2); Button btn3 = new Button(this); btn3.setText("按鈕3"); tr2.addView(btn3); tlayout.addView(tr2, tl_lpara); setContentView(tlayout); }效果圖如下:

透過程式碼動態建立佈局,特別適合需要依資料或條件即時產生介面的場景,也能幫助你更透徹地理解各佈局背後的 API 運作方式。
Android 自動亮度調節全流程拆解:從 Settings 到 PowerManager
手機能隨著環境光線自動調亮或調暗螢幕,靠的是 LIGHT 光線感應器。這條鏈路從設定界面一路延伸到系統服務,中間繞了不少彎,下面把整個流程理一遍。 一、從設定界面入手
Android JSON 解析教學:用 JSONObject 取出你要的欄位
JSON(JavaScript Object Notation)是一種輕量、獨立於語言的資料交換格式,語法比 XML 精簡得多,因此在行動端 API 的往來中幾乎已成標準
Android MediaPlayer 教學:播放、暫停與快轉的完整實作
Android 提供多種播放音訊與影片的方式,其中最通用的就是 MediaPlayer 類別。它被封裝在系統的媒體播放服務之上,讓你能用幾行程式碼就完成播放控制。 取得播
Android 登入畫面:表單驗證與錯誤次數限制
登入畫面的基本元素登入畫面通常用來收集使用者名稱與密碼。在 Android 中,常使用兩個 EditText 作為輸入欄位,並將密碼欄位設定為密碼輸入型態,避免內容被明顯