Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 教學:用程式碼實作常用佈局

編輯: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 運作方式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved