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

版面與控制項(六):TableLayout 與 VideoView 完整用法

編輯:關於 Android 程式設計

第 7 節 TableLayout

TableLayout 顧名思義,就是讓子元件像表格一樣排列的容器。它繼承自 LinearLayout,因此 LinearLayout 的屬性它通通能用。

7.1 TableLayout 的列(行數怎麼來)

TableLayout 必須搭配搭檔 TableRow 使用,而 TableRow 同樣是 LinearLayout 的子類,代表表格的「每一橫列」。三列的表格,版面大致長這樣:

<TableLayout>
    <TableRow> ... </TableRow> <!-- 第一列 -->
    <TableRow> ... </TableRow> <!-- 第二列 -->
    <TableRow> ... </TableRow> <!-- 第三列 -->
</TableLayout>

有個貼心的小細節:TableRow 可以省略 android:layout_height 與 android:layout_width。這跟其他版面不同 —— 其他容器少寫這兩個屬性,編譯器會直接報錯。省略時 TableRow 的預設值為 height = wrap_content、width = match_parent。

7.2 TableLayout 的欄(欄數怎麼算)

每一個 TableRow 裡包含的子控制項數量,就是欄數。若各列的子元件數量不一,則以最多的那一列為準,作為整個表格的欄數。

以下這個計算機鍵盤就是很好的例子:

<TableLayout>
    <TableRow>
        <Button android:text="C" />
        <Button android:text="DEL" />
        <Button android:text="." />
        <Button android:text="+" />
    </TableRow>
    <TableRow>
        <Button android:text="7" />
        <Button android:text="8" />
        <Button android:text="9" />
        <Button android:text="-" />
    </TableRow>
    <TableRow>
        <Button android:text="4" />
        <Button android:text="5" />
        <Button android:text="6" />
        <Button android:text="*" />
    </TableRow>
    <TableRow>
        <Button android:text="1" />
        <Button android:text="2" />
        <Button android:text="3" />
        <Button android:text="/" />
    </TableRow>
    <TableRow>
        <Button android:text="0" />
    </TableRow>
</TableLayout>

執行結果是一個 5×4 的表格:有的列 4 格、有的只有 1 格,於是欄數取最大值 4。由於此處螢幕空間較寬裕,表格並未填滿整個畫面。

7.3 TableLayout 的版面微調

7.3.1 讓列平均分配

希望每一列平均瓜分可用空間時,為每個 TableRow 加上 android:layout_weight="1" 即可。

7.3.2 讓欄平均分配:stretchColumns

要讓每一欄平均分配剩餘空間,在 TableLayout 上加 android:stretchColumns="*",星號代表所有欄位都參與拉伸。

也可以只拉伸部分欄位,例如指定第 2、4 欄:

android:stretchColumns="1,3"

請注意兩點:欄位序號從 0 開始計算;多個欄位以逗號分隔。

7.3.3 讓欄收縮:shrinkColumns

當某個格子的內容過長,會擠壓同一列其他欄的顯示。此時可以對 TableLayout 設定 android:shrinkColumns,指定可收縮的欄位,讓該欄內容往下方換行伸展。

android:shrinkColumns="0"

同樣地,序號從 0 起算,多欄以逗號分隔。

7.3.4 隱藏欄位:collapseColumns

要藏掉某一欄,使用 android:collapseColumns 並填入要隱藏的欄位序號。

可以看到第一欄已經消失不見了。

7.3.5 讓格子跨欄:layout_span

希望某個按鈕橫跨多欄時,在該子元件上使用 android:layout_span。以下讓數字鍵 0 跨兩欄:

<Button android:layout_span="2" android:text="0" />

要注意的是,TableLayout 的格子只能跨欄合拼,無法跨列合拼。

第 8 節 VideoView

要在 App 裡播放影片,Android SDK 提供了現成的 VideoView。它其實是對 MediaPlayer 與 Surface 的封裝,對初次接觸影片播放開發的人來說,是最簡單省事的選擇,不必操心底層細節。

8.1 VideoView 的基本用法

先在版面檔放一個 VideoView:

<VideoView
    android:id="@+id/video_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

接著在 Activity 取得它並指定影片來源,字串路徑與 URI 兩種方式都支援:

mVideoView = (VideoView) findViewById(R.id.video_view);
mVideoView.setVideoPath(strPath);  // 字串路徑
mVideoView.setVideoURI(uriPath);   // Uri 物件

字串路徑的形式類似 /storage/emulated/0/Video/mothernature.mp4。

播放流程的控制全靠 VideoView 提供的幾個方法:

mVideoView.start();      // 從頭開始播放
mVideoView.pause();      // 暫停
mVideoView.resume();     // 繼續
mVideoView.seekTo(xxx);  // 跳到 xxx 毫秒處

8.2 加上 MediaController 控制面板

VideoView 可以掛上一個 MediaController,它整合了進度拖動、暫停、繼續等功能,還能在無操作時自動隱藏。

MediaController controller = new MediaController(context);
mVideoView.setMediaController(controller);

不過有個實務上很常被踩到的細節:如果 VideoView 有父容器,MediaController 會被貼在父容器的底部,而不是貼在影片下方。

為了避免這種突兀的呈現,實務上常把 VideoView 單獨包進一個 FrameLayout 並置中:

<FrameLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <VideoView
        android:id="@+id/video_view"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />
</FrameLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved