編輯:關於 Android 程式設計
Android 的介面是由佈局與組件協同完成的。佈局好比建築中的框架,組件則相當於磚瓦;組件依佈局的要求排列,就組成了使用者看到的畫面。

所有的佈局方式都可以歸類為 ViewGroup 的五個直接子類,其他佈局都是從這五種擴展而來。
這是最常用也最簡單的一種。子元素依照垂直或水平方向依次排列,每個子元素位於前一個之後。垂直排列時是 N 列單欄結構,每一列只有一個元素,不論其寬度為何;水平排列時則是單列 N 欄結構。若想做出兩列兩欄的效果,一般做法是先垂直排列兩個元素,每個元素內部再各自包含一個水平排列的 LinearLayout。
子元素的 android:layout_weight 屬性用來描述它在剩餘空間中佔的比例。若一列只有一個文字方塊,預設值為 0;若一列有兩個等寬文字方塊,兩者的 weight 可以都設為 1;若希望比例為二比一,則分別設為 1 和 2,此時第一個佔剩餘空間的三分之二,第二個佔三分之一。原則是數值越小,重要性越高。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff000000"
android:text="Hello">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff654321"
android:text="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#fffedcba"
android:text="2" />
</LinearLayout>
</TextView>
</LinearLayout>
RelativeLayout 依各子元素之間的相對位置關係來排版,因此與位置相關的屬性會生效,例如 android:layout_below、android:layout_above、android:layout_centerVertical 等。注意:指定位置關係時,被引用的 ID 必須先被定義,否則會出現異常。
RelativeLayout 是五大佈局中最靈活的一種,比較適合排版較複雜的畫面。
<?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="fill_parent">
<TextView
android:id="@+id/text_01"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#ffffffff"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/text_02"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"
android:layout_centerHorizontal="true"
android:background="#ff654321"
android:gravity="center"
android:text="2" />
<TextView
android:id="@+id/text_03"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@id/text_01"
android:layout_toLeftOf="@id/text_02"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</RelativeLayout>
此佈局中,子元素的 android:layout_x 與 android:layout_y 屬性會生效,用來描述座標位置。螢幕左上角為原點 (0,0),第一個 0 是橫座標,向右增大;第二個 0 是縱座標,向下增大。子元素之間可以互相重疊。
實務開發中通常不採用這種方式,因為它的排版過於剛性,難以適配各種終端。
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="50dp"
android:layout_y="50dp"
android:background="#ffffffff"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="25dp"
android:layout_y="25dp"
android:background="#ff654321"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="125dp"
android:layout_y="125dp"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
FrameLayout 是五種佈局中最簡單的一種,也可以理解為「層疊」方式。整個畫面被當成一塊空白備用區域,所有子元素都放在左上角,後面的子元素會直接覆蓋在前面的子元素之上,將前者部分或全部遮擋。下例中,第一個 TextView 被第二個完全遮擋,第三個 TextView 又遮住了第二個的一部分。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff654321"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#fffedcba"
android:gravity="center"
android:text="3" />
</FrameLayout>
適用於 N 列 N 欄的排版。一個 TableLayout 由許多 TableRow 組成,一個 TableRow 就代表表格中的一列。
TableRow 是 LinearLayout 的子類。TableLayout 不需要明確宣告有幾列幾欄,而是透過 TableRow 與其他組件來控制表格的列數與欄數;每往 TableRow 裡新增一個組件,表格就多一欄。若直接把組件放進 TableLayout,該組件會自成一行。表格中各欄的寬度由該欄最寬的儲存格決定,整個表格的寬度則取決於父容器(預設佔滿父容器)。
由於 TableLayout 繼承自 LinearLayout,因此完全支援 LinearLayout 的所有 XML 屬性,此外還支援:
android:collapseColumns:設定要隱藏的欄序號,多個以逗號分隔。
android:shrinkColumns:設定要收縮的欄序號,多個以逗號分隔。
android:stretchColumns:設定允許被拉伸的欄序號,多個以逗號分隔。
<?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">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:gravity="center"
android:padding="10dp"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff654321"
android:gravity="center"
android:padding="10dp"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffedcba"
android:gravity="center"
android:padding="10dp"
android:text="3" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff654321"
android:gravity="center"
android:padding="10dp"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffedcba"
android:gravity="center"
android:padding="10dp"
android:text="3" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fffedcba"
android:gravity="center"
android:padding="10dp"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff654321"
android:gravity="center"
android:padding="10dp"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:gravity="center"
android:padding="10dp"
android:text="1" />
</TableRow>
</TableLayout>
Android 進階:BroadcastReceiver 靜態與動態註冊實例詳解
前面幾篇分別談了 Activity 和 Service,這次輪到第三大組件 BroadcastReceiver。廣播是 Android 應用之間互通消息的機制,它和 In
用 ViewPager 做出類似 QQ 的分頁切換效果
這幾天在研究 ViewPager,順手做了一個類似 QQ「最近聯繫人、好友、群組」那樣的分頁切換效果(至於 QQ 本身是否也用它實作,就不得而知了)。 ViewPager
ActivityThread 的 main 方法:Android 應用程式真正的起點
寫在前面:暴雨天還能去上課的是好學生,能去上班的是游泳健將。 先問兩個問題: 第一,在 Android 裡,一個應用程式的真正入口是什麼?第二,為什麼 Android
Android Studio 下百度地圖 SDK 初體驗全流程
取得 SHA1 之後,我們就開始進入百度地圖 SDK 的資源獲取環節。 一、獲取密鑰與 SDK 1、搜索百度地圖 SDK 2、點擊獲取密鑰 3、創建應用(注意此處填寫