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

Android 常用布局容器入門

編輯:Android 開發實例

Android 開發中最常打交道的三個容器是 LinearLayout、RelativeLayout 和 TableLayout。它們的排列邏輯各異,選對容器往往比堆疊嵌套更能簡化布局。

一、LinearLayout:線性排列

子組件或子容器按水平或垂直方向排成一條線。常用的五個屬性:

  • android:orientation:取值 horizontal 為水平排列,vertical 為垂直排列。
  • android:layout_width 與 android:layout_height:每個子元素都必須聲明。
  • android:layout_weight:按權重分配剩餘空間,控制各組件佔用的比例。
  • android:layout_gravity:控制子組件在排列方向上的對齊方式。
  • android:padding:內邊距,讓內容與邊緣保持距離。

一個簡單的示例:

<?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"
>
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:text="RadioButton1" />
<RadioButton
android:text="RadioButton2" />
</RadioGroup>
<RadioGroup
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:text="RadioButton1" />
<RadioButton
android:text="RadioButton2" />
<RadioButton
android:text="RadioButton3" />
</RadioGroup>
</LinearLayout>

二、RelativeLayout:相對定位

子組件的位置由「它與容器的關係」以及「它與其他組件的關係」共同決定,適合製作表單一類貼齊式界面。

組件相對於容器:android:layout_alignParentTop、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight、layout_centerHorizontal、layout_centerVertical、layout_centerInParent。

組件相對於組件:android:layout_above、layout_below、layout_toLeftOf、layout_toRightOf 等。使用這一類屬性時,被引用的控件必須先定義 android:id,否則會出現空引用。

示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px">
<TextView android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:paddingTop="15px"/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/label"
android:layout_alignBaseline="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignRight="@id/entry"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>

三、TableLayout:表格布局

把子元素放進行與列構成的網格中。每一行用 <TableRow> 標識,android:stretchColumns 指定哪些列可以拉伸以填滿剩餘寬度,android:layout_column 指定控件所在的列序號,android:layout_span 則用於合併多列。

<?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:stretchColumns="1">
<TableRow>
<TextView
android:text="URL:" />
<EditText android:id="@+id/entry"
android:layout_span="3"/>
</TableRow>
<View
android:layout_height="2px"
android:background="#0000FF" />
<TableRow>
<Button android:id="@+id/cancel"
android:layout_column="2"
android:text="Cancel" />
<Button android:id="@+id/ok"
android:text="OK" />
</TableRow>
</TableLayout>

實踐中一個常見建議是:盡量減少嵌套層數。多層 LinearLayout 疊套會明顯增加測量與布局的開銷,能一層 RelativeLayout 解決的,就不必疊三層。

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