Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 三大常用佈局詳解:線性、表格與相對佈局

編輯:關於 Android 程式設計

每當我們看到 Android 手機上呈現出精緻漂亮的頁面時,總會好奇它是如何做出來的。其實並不難,因為每一個頁面背後都必定有一個對應的佈局。本文將介紹 Android 開發中最常使用的三種佈局:線性佈局、表格佈局與相對佈局。無論哪種佈局,通常都有 XML 與程式碼兩種實作方式。

一、線性佈局(LinearLayout)

線性佈局透過 orientation 屬性控制元件是水平還是垂直排列。以下是垂直排列的 XML 範例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />
   <Button android:id="@+id/testButton4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/myRalativeLayoutButton3"/>
</LinearLayout>

雖然開發中大量採用 XML 方式,但掌握程式碼生成也很重要。特別是當頁面上要顯示的元件並不固定、需要依據使用者操作動態呈現時,用程式碼建立會方便許多。以下以程式碼動態建立線性佈局,並用一個按鈕控制單選框的顯示與隱藏:

public class myLinearLayout extends Activity{
 private boolean flag=true;
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  final LinearLayout layout=new LinearLayout(this);
  LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
  layout.setOrientation(LinearLayout.VERTICAL);
  LinearLayout.LayoutParams textParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  TextView tv=new TextView(this);
  tv.setLayoutParams(textParams);
  tv.setText(R.string.myLinearLayoutText);
  tv.setTextSize(20);
  layout.addView(tv);
  LinearLayout.LayoutParams buttonParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  Button button =new Button(this);
  button.setLayoutParams(buttonParams);
  button.setText(R.string.myLinearLayoutButton1);
  layout.addView(button,buttonParams);
  final RadioGroup radioGroup=new RadioGroup(myLinearLayout.this);
  final RadioButton radioButton1=new RadioButton(myLinearLayout.this);
  radioButton1.setId(1);
  radioButton1.setText(R.string.maleText);
  radioButton1.setChecked(true);
  radioGroup.addView(radioButton1);
  final RadioButton radioButton2=new RadioButton(myLinearLayout.this);
  radioButton2.setId(2);
  radioButton2.setText(R.string.famaleText);
  radioGroup.addView(radioButton2);
  layout.addView(radioGroup);
  radioGroup.setVisibility(View.GONE);
  super.setContentView(layout,layoutParams);
  button.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v) {
    if(flag){
     radioGroup.setVisibility(View.VISIBLE);
     flag=false;
    }else{
     radioGroup.setVisibility(View.GONE);
     flag=true;
    }
   }
  });
 }
}

其中 GONE 與 VISIBLE 分別用來隱藏與顯示元件。

二、表格佈局(TableLayout)

打開 Android 手機時,眼前呈現的往往就是一個表格佈局。以下用 XML 呈現一個包含表頭的表格:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical"
   android:id="@+id/myTableLayoutId"
   android:shrinkColumns="3"
   android:background="@drawable/psu">
   <TableRow>
      <TextView android:layout_column="0" android:gravity="center_vertical" android:padding="8px" android:text="ID"/>
      <TextView android:layout_column="1" android:gravity="center_vertical" android:padding="8px" android:text="name"/>
      <TextView android:layout_column="2" android:gravity="center_vertical" android:padding="8px" android:text="email"/>
      <TextView android:layout_column="3" android:padding="8px" android:gravity="center_vertical" android:text="address"/>
   </TableRow>
</TableLayout>

表格佈局與 Java Web 中的 table 概念相似,其中的 TableRow 相當於 tr。shrinkColumns 屬性用來控制當某一行內容過長無法完整顯示時,指定哪一列可以換行壓縮。此外,還能透過 android:gravity 控制每個 TableRow 內容的置中、靠左或靠右等對齊方式。

以下以程式碼動態組裝表格,重點在於透過索引 j 控制元件確實落在同一列中:

public class myTableLayoutTwo extends Activity {
 private Map<String, String> mapTitle = new HashMap<String, String>();
 private List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.mapTitle.put("id", "編號");
  this.mapTitle.put("name", "姓名");
  this.mapTitle.put("cj", "成績");
  for (int i = 0; i < 3; i++) {
   Map<String, String> map = new HashMap<String, String>();
   map.put("id", String.valueOf(i));
   map.put("name", "bruce" + i);
   map.put("cj", String.valueOf(i));
   this.listData.add(map);
  }
  TableLayout layout = new TableLayout(this);
  TableLayout.LayoutParams tableLayout = new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
  layout.setBackgroundResource(R.drawable.psu);
  TableRow tableRowTitle=new TableRow(this);
  for(int i=0;i<this.mapTitle.size();i++){
   String key="";
   switch (i) {
    case 0: key="id"; break;
    case 1: key="name"; break;
    case 2: key="cj"; break;
   }
   TextView textViewTitle=new TextView(this);
   textViewTitle.setText(this.mapTitle.get(key));
   tableRowTitle.addView(textViewTitle);
  }
  layout.addView(tableRowTitle);
  for(int i=0;i<this.listData.size();i++){
   Map<String,String> map=this.listData.get(i);
   TableRow tableRow=new TableRow(this);
   for(int j=0;j<map.size();j++){
    String key="";
    switch (j) {
     case 0: key="id"; break;
     case 1: key="name"; break;
     case 2: key="cj"; break;
    }
    TextView textViewContent=new TextView(this);
    textViewContent.setText(map.get(key));
    tableRow.addView(textViewContent,j);
   }
   layout.addView(tableRow);
  }
  super.setContentView(layout,tableLayout);
 }
}

三、相對佈局(RelativeLayout)

顧名思義,相對佈局需要一個參照物,才能相對它來排列元件。以下範例將 id 為 imgb 的圖片放在 imga 的右邊,透過 toRightOf 屬性控制:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:id="@+id/myRelativeLayoutId"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <ImageView android:id="@+id/imga"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/android_mldn_01"/>
 <ImageView android:id="@+id/imgb"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/android_mldn_02"
    android:layout_toRightOf="@id/imga"/>
</RelativeLayout>

程式碼實作時,透過 addRule 方法設定相對關係,例如 RIGHT_OF、BELOW 等:

public class myRelativeLayout extends Activity {
 private RelativeLayout myRelativeLayout;
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  super.setContentView(R.layout.myralativelayout);
  this.myRelativeLayout = (RelativeLayout) super.findViewById(R.id.myRelativeLayoutId);
  RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.imga);
  relativeLayoutParams.addRule(RelativeLayout.BELOW, R.id.myT1);
  Button button=new Button(this);
  button.setText(R.string.maleText);
  this.myRelativeLayout.addView(button,relativeLayoutParams);
 }
}

這裡要特別提醒:應避免在應用中出現硬編碼,像 button.setText("文字") 這種寫法最好改為在 string.xml 中定義,統一管理說明文字,例如 button.setText(R.string.maleText)。此外,由於手機的記憶體與 CPU 資源有限,開發時要權衡使用程式碼還是 XML 建立佈局,一般建議優先採用 XML。掌握以上三種佈局並靈活搭配巢狀使用,就能打造出幾乎所有想要的畫面效果。

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