編輯:關於 Android 程式設計
這次實驗只動 XML,Java 端完全不碰——純粹練習畫面排版。因為只是介面設計,所有想呈現的效果都能靠宣告式語法完成。
顧名思義,線性佈局把子元件沿著單一方向排成一條線,方向由 android:orientation 決定,可以是 vertical 也可以是 horizontal。
實驗中用到的屬性說明如下:
android:layout_weight:決定各控制項佔用空間的比例權重。
android:paddingLeft:左內距,也就是文字與控制項左邊界的距離,其他方向同理。
android:gravity:控制項內部文字對齊控制項本身的方式。
android:background:背景色,使用 rgb 值時前面要加「#」。
android:textSize:字級大小,單位是 pt。
android:id:控制項的識別名稱。
android:layout_width:控制項寬度,layout_height 則是高度。
另外補充一個單位觀念:dip 是與解析度無關的長度單位,能讓畫面在不同 dpi 的手機上保持一致比例,寫死 px 反而容易在不同裝置上跑掉。
實驗結果是兩行文字,各自套上不同的屬性組合:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/London"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="倫敦奧運"
android:textSize="20pt"
android:background="#00ff00"
android:gravity="center_horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:layout_weight="1" />
<TextView
android:id="@+id/China"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="中國加油!!!"
android:textSize="35pt"
android:background="#ff0000"
android:layout_weight="3" />
</LinearLayout>
第一行 weight 為 1、第二行為 3,所以紅色那塊的高度會是綠色那塊的三倍,這正是權重最直觀的用法。
TableLayout 的邏輯很像 HTML 的 table:可以定義多列,每一列又能切成多欄,橫平竖直一目了然,因此適合報表類型的畫面。
這次用到的屬性:
用 TableRow 新增一列,該列內的各欄會自動並排。
android:padding:四個方向的內距採用同一個數值。
android:stretchColumns:當該列需要填滿螢幕時,指定要拉伸的是第幾欄(從 0 開始數)。
實驗結果是三列、每列四欄:

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="國家" android:background="#848484" android:padding="2dip" />
<TextView android:text="金牌" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="銀牌" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="銅牌" android:background="#0000ff" android:padding="2dip" />
</TableRow>
<TableRow>
<TextView android:text="中國" android:background="#848484" android:padding="2dip" />
<TextView android:text="*" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="**" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="***" android:background="#0000ff" android:padding="2dip" />
</TableRow>
<TableRow>
<TextView android:text="美國" android:background="#848484" android:padding="2dip" />
<TextView android:text="*" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="**" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="***" android:background="#0000ff" android:padding="2dip" />
</TableRow>
</TableLayout>
這裡把 stretchColumns 設為 1,代表第二欄(金牌/星號那一欄)會自動撐開填滿剩餘寬度,避免各欄寬度不一致造成錯位。
混合佈局的原理其實和前面一樣,只是把「大佈局裡嵌小佈局,小佈局裡再嵌另一種佈局」這件事做起來。實務上的畫面幾乎不可能只靠單一種佈局完成,巢狀是常態。
這次的組合是:最外層為垂直 LinearLayout;上半部是一個垂直 LinearLayout,放兩行文字;下半部是一個水平 LinearLayout,裡面再塞一個 TableLayout。總共四層佈局。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 上半部:垂直線性佈局,weight 為 1 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/London"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="倫敦奧運"
android:textSize="5pt"
android:background="#00ff00"
android:gravity="center_horizontal"
android:padding="10pt"
android:layout_weight="1" />
<TextView
android:id="@+id/China"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="中國加油!!!"
android:textSize="8pt"
android:background="#ff00ff"
android:layout_weight="3" />
</LinearLayout>
<!-- 下半部:水平線性佈局包著表格佈局,weight 為 3 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="國家" android:background="#848484" android:padding="2dip" />
<TextView android:text="金牌" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="銀牌" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="銅牌" android:background="#0000ff" android:padding="2dip" />
</TableRow>
<TableRow>
<TextView android:text="中國" android:background="#848484" android:padding="2dip" />
<TextView android:text="*" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="**" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="***" android:background="#0000ff" android:padding="2dip" />
</TableRow>
<TableRow>
<TextView android:text="美國" android:background="#848484" android:padding="2dip" />
<TextView android:text="*" android:background="#ff0000" android:padding="2dip" />
<TextView android:text="**" android:background="#00ff00" android:padding="2dip" />
<TextView android:text="***" android:background="#0000ff" android:padding="2dip" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
巢狀佈局雖然靈活,但層數越深、量測與配置的成本越高。實務上會建議能扁平就扁平,能改用 RelativeLayout 或後來的 ConstraintLayout 解決的,就不必硬堆 LinearLayout。
這輪練習把 Activity 最基礎的兩種佈局摸了一遍:線性佈局靠 orientation 與 weight 分配空間,表格佈局靠 TableRow 與 stretchColumns 對齊欄寬,兩者再透過巢狀組合幾乎能拼出任何畫面。
作者:tornadomeet
微信第三方登入 Android 端實作解析
微信第三方登入讓使用者能快速進入 App,也減少重複註冊的成本。在 Android 端實作時,重點是取得授權碼,再交由伺服器完成後續驗證。 前置準備 首先需要在微信開放平
Android 應用整合百度統計的完整實踐
對於每一款自研的 App 來說,用戶活躍度與使用量是衡量產品價值的重要指標。透過數據統計,我們能即時掌握 App 的下載狀況與使用趨勢,為後續的產品決策提供可靠依據。 就
一分鐘搞懂 Android 的 ldpi、mdpi、hdpi 與 xhdpi
DPI 是「每英寸的像素數」,它決定了螢幕看起來有多細緻,也決定了同一個 dp 值在不同機種上會被換算成多少 px。 取得螢幕參數 DisplayMetrics met
一文吃透 Android 各種通信機制:組件、進程、線程與跨應用
什麼是通信? 通信,顧名思義,就是資訊的傳遞與交換。在 Android 這套龐大的體系裡,通信貫穿始終,是每一名開發者都必須跨過的門檻。 讀完本文,你將收穫什麼 依照目