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

安卓六大基礎佈局詳解:從線性到幀佈局

編輯:關於 Android 開發

安卓六大基礎佈局詳解:從線性到幀佈局

今天我們來系統梳理安卓最基礎的幾種佈局方式,它們是搭建每一個界面的基石:

1. 線性佈局(LinearLayout)
2. 相對佈局(RelativeLayout)
3. 表格佈局(TableLayout)
4. 網格佈局(GridLayout)
5. 絕對佈局(AbsoluteLayout)
6. 幀佈局(FrameLayout)

一、線性佈局(LinearLayout)

線性佈局會把子元素沿著單一方向(垂直或水平)依次排列,透過 layout_weight 分配剩餘空間,非常靈活。1.xml 文件配置:

<?xml version="1.0" encoding="utf-8"?> <!-- 線性布局的頁面 --> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#0000FF" android:gravity="center_horizontal|center_vertical" android:text="線性布局" android:textSize="20sp"/> <LinearLayout android:layout_width="200dp" android:layout_height="150dp" android:layout_gravity="center_horizontal|center_vertical" android:background="#0000FF" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="#00FF00" android:text="內容一" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#F7F709" android:text="內容二" /> </LinearLayout> <LinearLayout android:layout_width="200dp" android:layout_height="150dp" android:layout_gravity="right" android:background="#0000FF" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#00FF00" android:text="內容一" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:background="#F7F709" android:text="內容二" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按一" android:onClick="click" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按二" android:onClick="click"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按三" android:onClick="click"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="按四" android:onClick="click"/> </LinearLayout> </LinearLayout>

展示:

再看一個用它實現登入頁面的例子:

<?xml version="1.0" encoding="utf-8"?> <!-- 線性布局頁面 --> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center_horizontal|center_vertical" android:text="線性布局登錄頁面" android:textSize="20sp" /> <LinearLayout android:layout_width="200dp" android:layout_height="40dp" android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="賬號:" android:textSize="15sp" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> </LinearLayout> <LinearLayout android:layout_width="200dp" android:layout_height="40dp" android:layout_gravity="center_horizontal|center_vertical" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="密碼:" android:textSize="15sp" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> </LinearLayout> <LinearLayout android:layout_width="200dp" android:layout_height="30dp" android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="15dp" android:orientation="horizontal" > <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="記住賬號" /> <CheckBox android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="記住密碼" /> </LinearLayout> <LinearLayout android:layout_width="200dp" android:layout_height="30dp" android:layout_gravity="center_horizontal|center_vertical" android:layout_marginTop="15dp" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="click" android:text="登錄" android:textSize="10sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="click" android:text="注冊" android:textSize="10sp" /> </LinearLayout> </LinearLayout>

展示:

二、相對佈局(RelativeLayout)

相對佈局允許子元素相對於父容器或其他兄弟元素定位,靈活度很高。

<?xml version="1.0" encoding="utf-8"?> <!-- 相對布局頁面 --> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:text="相對布局登錄頁面" android:textSize="20sp" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textView" android:layout_marginLeft="80px" android:layout_marginTop="30dp" android:text="賬號:" android:textSize="15sp" /> <EditText android:id="@+id/firstEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/textView1" android:layout_marginLeft="130px" android:layout_marginRight="50px" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/firstEditText" android:layout_marginLeft="80px" android:layout_marginTop="20dp" android:text="密碼:" android:textSize="15sp" /> <EditText android:id="@+id/firstEditText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/textView2" android:layout_marginLeft="130px" android:layout_marginRight="50px" /> <CheckBox android:id="@+id/checkbox1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/firstEditText2" android:layout_marginLeft="100px" android:layout_marginTop="20dp" android:text="記住賬號" /> <CheckBox android:id="@+id/checkbox2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/checkbox1" android:layout_marginLeft="280px" android:layout_marginRight="50px" android:text="記住密碼" /> <Button android:id="@+id/cancelButton1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/checkbox2" android:layout_marginLeft="100px" android:layout_marginRight="290px" android:layout_marginTop="25dp" android:text="登錄" android:textSize="14sp" /> <Button android:id="@+id/confremButton2" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignBottom="@id/cancelButton1" android:layout_marginLeft="290px" android:layout_marginRight="100px" android:text="注冊" android:textSize="14sp" /> </RelativeLayout>

展示:

三、表格佈局(TableLayout)

表格佈局以行與列的方式排列元素,適合展示表格化的資料。

<?xml version="1.0" encoding="utf-8"?> <!-- 表格布局頁面 --> <TableLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" android:stretchColumns="2" > <TableRow> <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:gravity="center" android:text="序號" android:textSize="20sp" /> <TextView android:layout_width="60dp" android:layout_height="wrap_content" android:gravity="center" android:text="書名" android:textSize="20sp" /> <TextView android:gravity="center" android:text="內容" android:textSize="20sp" /> <TextView android:layout_width="60dp" android:layout_height="wrap_content" android:gravity="center" android:text="作者" android:textSize="20sp" /> </TableRow> <View android:layout_height="1.5dp" android:background="#00FF00" /> <TableRow> <TextView android:gravity="center" android:text="1" /> <TextView android:gravity="center" android:text="西游記" /> <TextView android:gravity="center" android:text="取經" /> <TextView android:gravity="center" android:text="鄭晨" /> </TableRow> <View android:layout_height="1.5dp" android:background="#00FF00" /> <TableRow> <TextView android:gravity="center" android:text="2" /> <TextView android:layout_column="2" android:gravity="center" android:text="孫悟空大鬧天空" /> </TableRow> <View android:layout_height="1.5dp" android:background="#00FF00" /> <TableRow> <TextView android:gravity="center" android:text="3" /> <TextView android:layout_column="1" android:gravity="center" android:text="盤龍" /> <TextView android:layout_column="3" android:gravity="center" android:text="鄭晨" /> </TableRow> <View android:layout_height="1.5dp" android:background="#00FF00" /> </TableLayout>

展示:

四、網格佈局(GridLayout)

網格佈局以網格方式排列子元素,適合製作計算機按鍵盤這類規則排列的介面。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <!-- 網格布局 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center" android:text="計算機" android:textSize="30sp" /> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:columnCount="4" android:rowCount="6" > <EditText android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:editable="false" android:focusable="false" android:gravity="right" android:text="0" android:textSize="20sp" /> <Button android:text="AC" android:onClick="acClick"/> <Button android:text="+/-" /> <Button android:text="%" /> <Button android:text="+" /> <Button android:text="7" /> <Button android:text="8" /> <Button android:text="9" /> <Button android:text="x" android:onClick="anClick" /> <Button android:text="4" /> <Button android:text="5" /> <Button android:text="6" android:onClick="bnClick" /> <Button android:text="-" /> <Button android:text="1" android:onClick="onClick" /> <Button android:text="2" /> <Button android:text="3" /> <Button android:text="+" /> <Button android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:text="0" /> <Button android:text="." /> <Button android:text="=" /> </GridLayout> </LinearLayout>

2.Activity 如下(這裡只寫了幾個按鍵可作為示例):

//網格布局 public class GridLayout extends Activity { private EditText editText; private boolean lastClickIsNumber = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grid_layout); editText = (EditText) findViewById(R.id.textView6); } public void acClick(View v) { } public void onClick(View v) { if (!lastClickIsNumber) { editText.setText("1"); } else { String oldContent = editText.getText().toString().trim(); oldContent += "1"; editText.setText(oldContent); } lastClickIsNumber = true; } public void anClick(View v) { if (!lastClickIsNumber) { editText.setText("x"); } else { String oldContent = editText.getText().toString().trim(); oldContent += "x"; editText.setText(oldContent); } lastClickIsNumber = true; } public void bnClick(View v) { if (!lastClickIsNumber) { editText.setText("6"); } else { String oldContent = editText.getText().toString().trim(); oldContent += "6"; editText.setText(oldContent); } lastClickIsNumber = true; } }

展示:

五、絕對佈局(AbsoluteLayout)

絕對佈局透過 layout_x 與 layout_y 精確指定每個元素的橫縱座標。由於缺乏自適應能力,實際開發中已很少使用,但了解它仍有助於理解佈局原理。

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_x="100dp" android:layout_y="50dp" android:text="你好嗎?"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_x="50dp" android:layout_y="100dp" android:text="我很好!"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_x="200dp" android:layout_y="100dp" android:text="真的嗎?"/> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_x="100dp" android:layout_y="200dp" android:text="真的!"/> </AbsoluteLayout>

展示:

關於幀佈局(FrameLayout),它是最簡單的佈局,所有子元素都堆疊在左上角,層層覆蓋,通常用於需要圖層重疊的場景。

到此為止,六大基礎佈局就介紹完了。接下來我會繼續介紹安卓的基本控件。

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