編輯:關於 Android 程式設計
操作電視時,使用者體驗的關鍵在於直接的人機介面——遙控器。當你為電視最佳化 Android 應用時,應特別留意使用者如何以遙控器而非觸控螢幕在應用中導航。
本課程說明如何為 TV 最佳化導航:確保所有版面控制項都能以方向鍵操作、為 UI 導航提供明顯的回饋,並將控制項放在易於取用的位置。
在電視上,使用者以遙控器的方向鍵或方向鍵操作,這將移動限制在上下左右四個方向。要打造優秀的 TV 應用,你必須提供使用者能快速上手的導航方案。設計方向鍵導航時,請遵循:
Android 通常會自動處理版面元素間的導航順序,多數情況你無須額外處理。若版面使導航變得困難,或你希望使用者以特定方式走訪版面,可為控制項設定明確的導航。例如對 android.widget.EditText,定義下一個取得焦點的控制項:
<EditText android:id="@+id/LastNameField"
android:nextFocusDown="@+id/FirstNameField" />
下表列出所有可用的導航屬性:
nextFocusDown:定義使用者向下導航時下一個取得焦點的 View。nextFocusLeft:定義向左導航時下一個取得焦點的 View。nextFocusRight:定義向右導航時下一個取得焦點的 View。nextFocusUp:定義向上導航時下一個取得焦點的 View。使用這些明確導航屬性時,將值設為版面中另一個控制項的 ID(android:id)。建議把導航順序設成迴圈,讓最後一個控制項把焦點導回第一個。
註:只有在系統套用的預設順序不理想時,才應使用這些屬性來修改導航順序。
為 UI 中所有可導航、可選取的元素套用合適的顏色高亮,讓使用者以方向鍵導航時能清楚知道目前哪個控制項被聚焦或選取,並在整個應用中採用一致的高亮方案。
Android 提供了 Drawable State List Resources 來實作選取與聚焦的高亮。例如:
res/drawable/button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
版面 XML 可將上述 state list drawable 套用於 Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />
請在可聚焦、可選取的控制項內留出足夠的內距(padding),讓高亮邊框能清楚呈現。
使用者應能以幾次方向鍵點擊抵達任何 UI 控制項,導航須簡單且直觀。對於任何不直觀的操作,請以文字說明協助使用者,例如透過說明按鈕或動作列圖示觸發的對話框。
預判使用者下一步想前往的畫面,並提供一鍵導航。若目前畫面的 UI 過於稀疏,可考慮改為多窗格畫面,並以 Fragment 實作。例如將左側放七大洲名稱、右側放各洲景點清單的多窗格 UI:
上述 UI 由 left_side_action_controls、continents、places 三個 Fragment 組成,其版面 XML 如下:
res/layout/cool_places.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/left_side_action_controls"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:layout_weight="0.2"/>
<fragment
android:id="@+id/continents"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:layout_weight="0.2"/>
<fragment
android:id="@+id/places"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_marginLeft="10dip"
android:layout_weight="0.6"/>
</LinearLayout>
這類多窗格 UI 讓方向鍵導航更輕鬆,並善用電視的水平螢幕空間。此外請注意,上例把動作控制項放在垂直捲動清單的左側,以便以方向鍵輕鬆取用。一般而言,對於含水平捲動元件的版面,應把動作控制項置於左右兩側;反之,對於垂直捲動元件則置於上下。
Android draw9patch(.9 圖)製作與使用詳解
.9 圖(NinePatch)是 Android 平台上特有的一種可拉伸圖片格式,廣泛應用於按鈕、對話框等需要自適應大小的背景。掌握它,能讓你的界面在不同分辨率下都保持美
android:layout_weight 深入解析:五組對照實驗看懂分配規則
layout_weight 是 LinearLayout 最常用的屬性之一,但它的行為並不像表面那樣直白——當子元素的 layout_width 不是 0dp 時,分配結
Android 多媒體框架(二十三):MediaCodec 軟硬解之辨與 MediaMuxer 實戰剪輯視頻
前幾章一直在啃 MediaCodec 的源碼,收到不少提問:MediaCodec 到底是硬解碼還是軟解碼?這個問題值得單獨說清楚。今天的主題包括:MediaCodec 的
Android 官方入門文件 [13]:暫停與恢復一個 Activity
Pausing and Resuming an Activity(暫停與恢復一個 Activity) 本節教你:1. 暫停你的 Activity2. 恢復你的 Activ