編輯:關於 Android 程式設計
TableLayout 顧名思義,就是讓子元件像表格一樣排列的容器。它繼承自 LinearLayout,因此 LinearLayout 的屬性它通通能用。
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。
每一個 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。由於此處螢幕空間較寬裕,表格並未填滿整個畫面。
希望每一列平均瓜分可用空間時,為每個 TableRow 加上 android:layout_weight="1" 即可。
要讓每一欄平均分配剩餘空間,在 TableLayout 上加 android:stretchColumns="*",星號代表所有欄位都參與拉伸。
也可以只拉伸部分欄位,例如指定第 2、4 欄:
android:stretchColumns="1,3"
請注意兩點:欄位序號從 0 開始計算;多個欄位以逗號分隔。
當某個格子的內容過長,會擠壓同一列其他欄的顯示。此時可以對 TableLayout 設定 android:shrinkColumns,指定可收縮的欄位,讓該欄內容往下方換行伸展。
android:shrinkColumns="0"
同樣地,序號從 0 起算,多欄以逗號分隔。
要藏掉某一欄,使用 android:collapseColumns 並填入要隱藏的欄位序號。
可以看到第一欄已經消失不見了。
希望某個按鈕橫跨多欄時,在該子元件上使用 android:layout_span。以下讓數字鍵 0 跨兩欄:
<Button android:layout_span="2" android:text="0" />
要注意的是,TableLayout 的格子只能跨欄合拼,無法跨列合拼。
要在 App 裡播放影片,Android SDK 提供了現成的 VideoView。它其實是對 MediaPlayer 與 Surface 的封裝,對初次接觸影片播放開發的人來說,是最簡單省事的選擇,不必操心底層細節。
先在版面檔放一個 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 毫秒處
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>
Android四大組件之Service進階
一、前言 上一節我們學習了 Service 的生命周期,以及啟動 Service 的兩種方法。本節將繼續深入了解 Service 中的 IntentService,並通過
Android 菜鳥筆記(3):Activity 之間的切換與數據傳遞
在 Android 中,每個 Activity 通常對應屏幕上的的一個界面(窗口級別的 Activity 除外),因此手機上兩個界面之間的切換,本質上就是 Activit
Android 學習筆記:EditText、TextView、Button 與選單欄實作
這篇筆記做一個小練習:兩個輸入框、一個運算符號、一顆按鈕,按下後跳到結果頁顯示乘積,同時加上「關於」與「退出」兩個選單項。基本元件全都用上了。 一、主畫面 acti
不想Root也能清掉預裝App?三星禁用應用教學
不想Root又要處理三星預裝APP,可用禁用應用程序功能。在程式列表長按不需要的APP,拖到禁用應用程序即可,效果接近移除。 想恢復時,到設定、應用程式管理、已關閉中重新