Android開發教學
Android 開發環境配置
Android 架構
Android 應用組件
Android Hello World示例
Android 資源組織和訪問
Android Activity
Android Service
Android廣播接收器
Android內容提供者
Android碎片/片段
Android Intent過濾器
Android UI佈局
Android LinearLayout
Android RelativeLayout
Android TableLayout
Android AbsoluteLayout
Android FrameLayout
Android ListView
Android GridView
Android UI控件
Android TextView
Android EditText
Android AutoCompleteTextView
Android Button
Android ImageButton
Android CheckBox
Android ToggleButton
Android RadioButton
Android RadioGroup
Android事件處理
Android樣式和主題
Android樣式示例
Android主題示例
Android自定義組件
Android拖放
Android通知
Android基於位置服務
Android發送電子郵件
Android發送短信/SMS
Android撥打電話
發佈Android應用
ArrayAdapter
SimpleCursorAdapter
Android ProgressDialog
Android Spinner
使用活動代碼自定義Android組件
使用佈局文件自定義Android組件
Android自定義組件及屬性
Android Alertdialog(警告對話框)
Android Animation(動畫)實例
Android音頻捕獲(錄音)
Android音頻管理器實例
Android AutoCompleteTextView(自動完成)實例
Android最佳實踐
Android Bluetooth(藍牙)實例
Android Camera(攝像頭)
Android Clipboard(複製/剪貼板)
Android自定義字體
Android數據備份
Android Gestures/手勢
Android圖片效果
Android圖片切換
Android內部存儲
Android JetPlayer實例
Android JSON解析器
Android加載Spinner
Android本地化
Android登錄實例
Android MediaPlayer(多媒體播放)

Android UI佈局

用戶界面的基本構建塊是創建View類View對象,並佔據屏幕上的一個矩形區域,負責繪圖和事件處理。View是用於創建交互式UI組件,它是按鈕,文本框等部件的基類。

ViewGroup是View的一個子類,並提供了無形的容器,容納其他視圖或其他ViewGroup定義佈局屬性。

第三個層次,不同的佈局是ViewGroup類的子類,一個典型的佈局定義爲 Android 用戶界面,並且可以在運行時創建,使用 View/ViewGroup 對象可視結構或者可以聲明佈局,使用簡單的XML文件main_layout.xml,這個文件在項目res/layout文件夾中。

本教程是更多是關於創建基於圖形用戶界面XML文件的佈局定義。佈局可以包含任何類型的部件,如按鈕,標籤,文本框等等。以下是一個簡單的XML文件的LinearLayout 例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Button" />

<!-- More GUI components go here  -->

佈局定義之後,可以從應用程序代碼加載的佈局資源,在Activity.onCreate()回調實現,如下所示:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Android佈局類型

有一些Android提供的佈局,可以使用在幾乎所有的Android應用程序提供不同的視圖,外觀和風格。

S.N.

佈局和說明

1

Linear Layout
LinearLayout視圖組,所有的子視圖在單一的方向對齊,垂直或水平。

2

Relative Layout
RelativeLayout相對位置顯示子視圖的視圖組。

3

Table Layout
TableLayout一種視圖,組視圖分爲行和列。

4

Absolute Layout
AbsoluteLayout 使能夠指定其子視圖的確切位置。

5

Frame Layout
FrameLayout 屏幕上是一個佔位符,可以用它來顯示一個單一的視圖。

6

List View
ListView顯示滾動項目列表視圖組。

7

Grid View
GridView 網格控件是一種ViewGroup用於顯示一個二維,滾動的網格的項目。

佈局屬性

每個佈局都有一組屬性,它定義佈局的視覺屬性。所有佈局中,有幾個共同的屬性和其他屬性佈局。以下是常見的屬性並可以應用到所有的佈局中:

屬性

描述

android:id

唯一地標識該視圖的ID

android:layout_width

佈局的寬度

android:layout_height

這是佈局的高度

android:layout_marginTop

這是關於佈局的頂側的額外的空間

android:layout_marginBottom

在佈局上的底側的額外的空間

android:layout_marginLeft

在佈局上的左側的額外的空間

android:layout_marginRight

在右側的佈局的額外空間

android:layout_gravity

它指定子視圖被定位

android:layout_weight

指定有多少佈局額外的空間應該分配給視圖

android:layout_x

指定佈局的x座標

android:layout_y

此指定佈局的y座標

android:layout_width

佈局的寬度

android:layout_width

佈局的寬度

android:paddingLeft

佈局的左填充

android:paddingRight

佈局的右填充

android:paddingTop

佈局頂部填充

android:paddingBottom

佈局底部填充

這裏佈局/視圖的寬度和高度的尺寸可以指定在DP(密度獨立像素),SP(規模獨立像素),PT(點爲1/72英寸),PX(像素),mm(毫米),或(英寸)。

可以指定寬度和高度精確的測量,但更多的時候,使用這些常量作爲寬度或高度設置:

  • android:layout_width=wrap_content 告訴視圖,其內容所需要的尺寸大小本身。

  • android:layout_width=fill_parent 告訴視圖如其父視圖一樣尺寸大小。

Gravity 屬性定位視圖對象中起着重要的作用,它可以採取一個或多個(「|」分隔)的恆定值,具體如下:

常量

描述

top

0x30

推進對象到其容器的頂部,不改變其大小

bottom

0x50

推進對象到其容器的底部,不改變其大小

left

0x03

推進對象到其容器的左側,不改變其大小

right

0x05

推進對象到其容器的右側,不改變其大小

center_vertical

0x10

放置對象在其容器的垂直中心,在不改變其大小

fill_vertical

0x70

如果需要的話完全填滿其容器增長對象的垂直尺寸

center_horizontal

0x01

放置對象在其容器的水平中心,不改變其大小

fill_horizontal

0x07

如果需要的話完全填滿其容器增長的對象的水平尺寸

center

0x11

放置對象在其容器的同時在垂直和水平軸的中心,而不改變其大小

fill

0x77

如果需要的話完全填滿其容器增長的對象的水平和垂直尺寸

clip_vertical

0x80

可以設置附加的選項,使頂部和/或子視圖底部邊緣夾在其容器的邊界。剪輯將基於垂直重力:頂重力將剪輯底部邊緣,一個底重力將夾的頂邊,並也不會夾兩側邊緣

clip_horizontal

0x08

可以設置附加的選項,使左和/或子視圖右邊緣夾在其容器的邊界。剪輯將根據水平重力:左重力將剪輯的右邊緣,右重力將剪輯的左邊緣,並且也不會夾兩邊

start

0x00800003

推進對象到其容器的開頭,不改變其大小

end

0x00800005

推進對象到其容器的末尾,不改變其大小

視圖標識

一個視圖對象有一個唯一的ID分配給它,用於唯一識別視圖。ID在XML標籤的語法是:

android:id="@+id/my_button"

以下是@+ 符號的簡要說明:

  • 在符號(@)開頭的字符串表示XML解析器解析和擴展ID字符串的其餘部分,將其識別爲一個ID的資源。

  • 加號(+)表示,這是一個新的資源名,必須創建並添加到資源中。要創建一個視圖對象的實例,並捕捉到它的佈局,使用以下命令:

Button myButton = (Button) findViewById(R.id.my_button);