編輯:關於 Android 程式設計
所謂嵌套布局,就是在同一個布局文件中把多個布局容器互相套疊。結構示意如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
</FrameLayout>
</LinearLayout>
下面用它來做一個計算器的界面。
整體分兩塊:上方是顯示區(多行文本,用垂直 LinearLayout 按權重分層),下方是鍵盤區(多行多列的按鈕矩陣)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 顯示區:佔 3 份權重 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="0dp" />
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:padding="0dp" />
</LinearLayout>
<!-- 功能鍵行 -->
<Button android:id="@+id/left_bracket_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text="("
android:textColor="@android:color/white" android:textSize="25sp" />
<Button android:id="@+id/right_bracket_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text=")"
android:textColor="@android:color/white" android:textSize="25sp" />
<Button android:id="@+id/divisor_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text="÷"
android:textColor="@android:color/white" android:textSize="25sp" />
<Button android:id="@+id/backspace_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#ffa500" android:text="C"
android:textSize="25sp" />
<!-- 7 8 9 × -->
<Button android:id="@+id/seven_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="7" />
<Button android:id="@+id/eight_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="8" />
<Button android:id="@+id/nine_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="9" />
<Button android:id="@+id/product_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text="×"
android:textColor="@android:color/white" android:textSize="25sp" />
<!-- 4 5 6 − -->
<Button android:id="@+id/four_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="4" />
<Button android:id="@+id/five_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="5" />
<Button android:id="@+id/six_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="6" />
<Button android:id="@+id/minus_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text="-"
android:textColor="@android:color/white" android:textSize="25sp" />
<!-- 1 2 3 + -->
<Button android:id="@+id/one_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="1" />
<Button android:id="@+id/two_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="2" />
<Button android:id="@+id/three_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="@android:color/darker_gray" android:text="3" />
<Button android:id="@+id/plus_button"
android:layout_height="fill_parent" android:layout_weight="0.25"
android:background="#333333" android:text="+"
android:textColor="@android:color/white" android:textSize="25sp" />
<!-- 底行:小數點、0、等號 -->
<Button android:id="@+id/point_button"
android:layout_height="fill_parent" android:layout_weight="0.5"
android:background="@android:color/darker_gray" android:text="." />
<Button android:id="@+id/zero_button"
android:layout_height="fill_parent" android:layout_weight="0.5"
android:background="@android:color/darker_gray" android:text="0" />
<Button android:id="@+id/equal_button"
android:layout_height="fill_parent" android:layout_weight="3.1"
android:background="#00CC00" android:text="=" />
</LinearLayout>
上面的代碼反復用到 android:layout_weight,要真正掌握這套布局,必須搞懂權重。
權重的本質是「按比例分配剩餘空間」。在垂直 LinearLayout 中,如果所有子元素都寫 layout_height="fill_parent"(或 0dp)並各自帶權重,父容器會先扣除固定尺寸,再把剩下的空間按權重比例切分給各子元素。
因此本例如:顯示區權重 3,功能鍵每行 0.25 × 4 = 1,五個鍵盤行合計 5,加上底行 0.5 + 0.5 + 3.1,總權重約為 12.6。顯示區佔了將近四分之一的屏高,視覺上比例剛好。
兩個實務要點:
這個計算器鍵盤其實是典型的網格結構。若用今天的工具重寫,GridLayout 會比嵌套 LinearLayout 直觀得多:只需設置 columnCount="4",再讓「=」鍵 layout_columnSpan="3" 即可,不必靠權重湊比例。若按鈕數量會動態變化,則可考慮 RecyclerView 搭配 GridLayoutManager。
下節預告:滾動視圖 ScrollView。
Android 滑動效果七種實作方式完整解析
搞懂坐標系與觸控事件之後,下一步就是讓 View 真的「動起來」。無論用哪一種方案,底層思路都是同一套:手指按下時記下座標,移動時再記一次,兩者相減得到偏移量,用偏移量去
Android 掛斷來電實作:用 AIDL 打造來電黑名單攔截
一、為什麼要繞道 AIDL Android 並沒有對外公開「結束通話」的 API。如果要在程式裡掛斷電話,必須透過 AIDL 與電話管理服務(Telephony)溝通,再
Android BroadcastReceiver 應用詳解
什麼是 BroadcastReceiverBroadcastReceiver 用於接收系統或應用發出的廣播。開機完成、網路變化、電量變動等事件,都可以透過廣播通知 App
Android 5.0 Material Design(四):用 elevation 定義陰影與裁剪 View
原文:developer.android.com/training/material/shadows-clipping 深度(Depth)帶來的資訊 Material D