編輯:關於 Android 程式設計
佈局是 UI 開發的地基。本文用可運行的小例子,把四種最常見的佈局容器一次講清楚。
LinearLayout 最關鍵的屬性是 android:orientation:vertical 代表垂直排列,horizontal 代表水平排列。
在 activity_main 中放三個按鈕,分別用兩種方向排列看看:
android:orientation="vertical"
android:orientation="horizontal"
按鈕還可以加上 android:layout_gravity,用來控制單一元件在佈局中的對齊方式。在三個按鈕上分別加入:
android:layout_gravity="top" android:layout_gravity="center_vertical" android:layout_gravity="bottom"
效果如下:
接著把按鈕移走,換成一個 EditText 加一個 Button:
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something" />
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 1" />
這裡有個關鍵細節:EditText 的寬度設為 0dp,此時它的實際寬度由 android:layout_weight 決定。系統會把同一層級的 weight 值加總,再依比例分配空間。如果把 Button 改回 wrap_content、EditText 維持 weight 寫法,Button 就照內容寬度計算,EditText 則吃滿剩下的部分:
把根佈局改成 RelativeLayout,放五個按鈕:
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="button 1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="button 2" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button 3" />
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="button 4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="button 5" />
android:layout_alignParent*** 這一組屬性決定元件相對於父佈局的位置。例如 button_1 的左與上設為 true,它就貼在左上角;button_3 用 layout_centerInParent 置中。
除了對齊父佈局,也可以相對於其他元件定位:
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="button 1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="button 2" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="button 3" />
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="button 4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="button 5" />
寫法和對齊父佈局幾乎一樣,只是把「父佈局」換成「某個元件的 id」。
這個佈局非常簡單,實際專案裡用得也少。它的特性是所有子元件都疊在左上角,後續講 Fragment 時會再用到它。
用表格的方式排列元件:
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/edit_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/edit_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your password" />
</TableRow>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
其中 android:layout_span="2" 讓這個按鈕佔據兩列的寬度。
TableLayout 無法直接指定各欄寬度,所以右邊會空出一塊。要解決這個問題,可以用 android:stretchColumns:
android:stretchColumns="1"
它表示當表格沒佔滿螢幕時,讓第 1 個索引(也就是第二欄)自動拉伸填補;若值為 "0",則拉伸第一欄。
實際開發中,這四種佈局通常不會單打獨鬥,而是多層嵌套:外層用 RelativeLayout 定骨架,內部用 LinearLayout 排一列資料,需要表格呈現時再套 TableLayout。掌握每種佈局的定位邏輯,比記住屬性名稱更重要。
AndroidManifest.xml 深度解析:activity 標籤的所有屬性
語法與位置 <manifest> <application> <activity android:name=str
Android ListView 快速返回頂部功能實作詳解
效果展示實作思路這個功能的核心,是在 ListView 不在頂部時顯示返回頂部按鈕,回到頂部後隱藏按鈕。因此需要使用 RelativeLayout 讓按鈕固定在右下角,並
Netty 實戰(二十三)第十二章 WebSocket:打造即時聊天室
第三篇:網絡協議 WebSocket 是一項為提升網絡性能與 Web 應用響應速度而生的協議。本章我們將重點介紹 Netty 對 WebSocket 的支持
Android DrawerLayout 側滑布局詳解(二):ActionBarDrawerToggle
ActionBarDrawerToggle:上一篇我們並沒有使用 drawLayout.setDrawerListener(),它對應的參數對象就是 DrawerLayo