Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

從零開始學 Android(十七):用嵌套布局實現計算器界面

編輯:關於 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 布局

<?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>

三、模擬器效果

四、理解 layout_weight:這套布局的靈魂

上面的代碼反復用到 android:layout_weight,要真正掌握這套布局,必須搞懂權重。

權重的本質是「按比例分配剩餘空間」。在垂直 LinearLayout 中,如果所有子元素都寫 layout_height="fill_parent"(或 0dp)並各自帶權重,父容器會先扣除固定尺寸,再把剩下的空間按權重比例切分給各子元素。

因此本例如:顯示區權重 3,功能鍵每行 0.25 × 4 = 1,五個鍵盤行合計 5,加上底行 0.5 + 0.5 + 3.1,總權重約為 12.6。顯示區佔了將近四分之一的屏高,視覺上比例剛好。

兩個實務要點:

  • 權重可以寫小數。底行的 0.5 / 0.5 / 3.1 正是為了讓「=」鍵佔據約 3/4 寬度,而「.」與「0」各佔 1/8。
  • 搭配權重時,尺寸建議寫 0dp 而非 fill_parent。寫 0dp 可以避免先按 fill_parent 計算再分配的冗餘測量,性能更好,也更符合直覺。

五、更現代的替代方案

這個計算器鍵盤其實是典型的網格結構。若用今天的工具重寫,GridLayout 會比嵌套 LinearLayout 直觀得多:只需設置 columnCount="4",再讓「=」鍵 layout_columnSpan="3" 即可,不必靠權重湊比例。若按鈕數量會動態變化,則可考慮 RecyclerView 搭配 GridLayoutManager。

下節預告:滾動視圖 ScrollView。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved