編輯:Android 開發實例
Android 的畫面結構由 Layout 決定。早期版本提供了五種主要容器,各有其定位邏輯:線性排列、層層覆疊、表格分割、絕對座標與相對關係。理解每種容器的特性,才能選對工具而不是硬湊。
layout_width 與 layout_height 決定尺寸,有三種寫法:fill_parent 表示跟著父容器走、wrap_content 表示依內容大小決定、或直接給一個具體數值(如 100px)。這兩個屬性幾乎每個 View 都必須設定,漏掉會導致版面配置失敗。
<?xml version="1.0" encoding="utf-8"?>
<!--
layout_width - 寬。fill_parent:跟著父元素走;wrap_content:跟著本身內容走;也可直接指定 px 值
layout_height - 高。同上
-->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- FrameLayout - 層疊式配置,以左上角為起點,一層覆蓋一層 -->
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="FrameLayout">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Frame Layout">
</TextView>
</FrameLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello" />
<!--
TableLayout - 表格式配置。
TableRow - 表格內的列,列內每個元件算一列(欄)
collapseColumns - 需隱藏的欄索引,多個以 , 隔開
stretchColumns - 需拉伸填滿可用空間的欄索引
shrinkColumns - 需自動收縮以免其他欄被擠出螢幕的欄索引
-->
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:collapseColumns="1">
<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="行1列1" />
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="行1列2" />
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="行1列3" />
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="行2列1" />
</TableRow>
</TableLayout>
<!--
AbsoluteLayout - 絕對定位配置。
layout_x - x 座標,以左上角為原點
layout_y - y 座標,以左上角為原點
-->
<AbsoluteLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="AbsoluteLayout"
android:layout_x="100px"
android:layout_y="100px" />
</AbsoluteLayout>
<!--
RelativeLayout - 相對定位配置。
layout_centerInParent - 置於容器水平與垂直中央(類似還有 layout_centerHorizontal、layout_alignParentLeft 等)
layout_marginLeft - 相對於容器左緣的距離
layout_below - 放到指定元件下方
layout_alignRight - 與指定元件右緣對齊
-->
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/abc"
android:layout_height="wrap_content" android:text="centerInParent=true"
android:layout_centerInParent="true" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="marginLeft=20px"
android:layout_marginLeft="20px" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="xxx"
android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello Layout</string>
<string name="app_name">webabcd_layout</string>
</resources>
版面配置寫好後,Activity 只需要一行 setContentView 就把畫面掛上,這正是「畫面描述與程式邏輯分離」的價值。
package com.webabcd.layout;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
實務上的優先順序是:能用 LinearLayout 解決就用它(效能最好、階層最淺);需要跨欄跨列對齊時用 RelativeLayout 或 TableLayout;FrameLayout 只留給單純的覆疊需求;AbsoluteLayout 除了相容舊專案,不建議再用。此外,版面階層越深,measure 與 layout 的開銷越大,保持淺而扁平是效能的基本原則。
Android 天氣 App 教學:JSON 資料到底怎麼讀
做過網路相關的 App 就會知道,伺服器回傳的資料幾乎都是 JSON。它是 JavaScript Object Notation 的縮寫,憑藉著結構清晰、沒有冗餘標籤這兩
Android MediaPlayer 多媒體播放教學
Android 提供了多種控制音訊與影片播放的方式,無論是播放本機檔案或串流媒體都能勝任,而其中最常被使用的,莫過於 MediaPlayer 這個類別。 Android
Android 接入新浪微博、騰訊微博與人人網的 OAuth 2.0 實作
許多 Android 應用會需要接入社交平台的分享、登入或發文能力。本文以新浪微博、騰訊微博與人人網為例,說明整合時最關鍵的授權流程,以及如何取得後續呼叫 API 所需的
Android JSON 剖析教學:JSONObject 與天氣資料
在 Android 開發中,JSON 解析是取得遠端資料的關鍵步驟。相較於 XML,JSON 更精簡,也更容易與 Web API 搭配。 先找出需要的欄位 解析前,先確認