本課程將說明如何建立實作,以反映新版 API 目前支援舊裝置的實作方式。
決定替代解決方案
要以回溯相容的方式使用新版 UI 功能,最棘手的任務是決定並實作舊版 (備用) 解決方案,以用於舊版平台。在許多情況下,只要使用舊版 UI 架構功能,即可實現這些新版 UI 元件的用途。例如:
-
使用包含圖片按鈕的橫向
LinearLayout
實作動作列,可以是自訂標題列,或做為活動版面配置的檢視畫面。溢位動作會顯示在裝置的「選單」按鈕下方。 -
您可以使用含有按鈕的水平
LinearLayout
或TabWidget
UI 元素,實作動作列分頁。 -
NumberPicker
和Switch
小工具可以分別使用Spinner
和ToggleButton
小工具實作。 -
您可以使用
PopupWindow
小工具實作ListPopupWindow
和PopupMenu
小工具。
通常沒有一體適用的解決方案,無法將較新的 UI 元件向後移植到舊裝置。留意使用者體驗:在舊型裝置上,使用者可能不熟悉較新的設計模式和 UI 元件。請花點時間思考,如何使用熟悉的元素來提供相同功能。在許多情況下,這通常不是問題:如果應用程式生態系統中比較有顯眼的 UI 元件 (例如動作列),或是互動模型極為簡單直覺 (例如使用 ViewPager
的滑動檢視畫面),
使用舊版 API 實作分頁
如要建立舊版動作列分頁實作,您可以使用 TabWidget
和 TabHost
(但可以改用水平配置的 Button
小工具)。在名為 TabHelperEclair
和 CompatTabEclair
的類別中實作此方法,因為此實作會使用 Android 2.0 (Eclair) 之前推出的 API。
CompatTabEclair
實作會將分頁屬性 (例如分頁文字和圖示) 儲存在執行個體變數中,因為沒有可用來處理這類儲存空間的 ActionBar.Tab
物件:
Kotlin
class CompatTabEclair internal constructor(val activity: FragmentActivity, tag: String) : CompatTab(tag) { // Store these properties in the instance, // as there is no ActionBar.Tab object. private var text: CharSequence? = null ... override fun setText(resId: Int): CompatTab { // Our older implementation simply stores this // information in the object instance. text = activity.resources.getText(resId) return this } ... // Do the same for other properties (icon, callback, etc.) }
Java
public class CompatTabEclair extends CompatTab { // Store these properties in the instance, // as there is no ActionBar.Tab object. private CharSequence text; ... public CompatTab setText(int resId) { // Our older implementation simply stores this // information in the object instance. text = activity.getResources().getText(resId); return this; } ... // Do the same for other properties (icon, callback, etc.) }
TabHelperEclair
實作會利用 TabHost
小工具上的方法建立 TabHost.TabSpec
物件和分頁指標:
Kotlin
class TabHelperEclair internal constructor(activity: FragmentActivity) : TabHelper(activity) { private var tabHost: TabHost? = null ... override fun setUp() { // Our activity layout for pre-Honeycomb devices // must contain a TabHost. tabHost = tabHost ?: mActivity.findViewById<TabHost>(android.R.id.tabhost).apply { setup() } } override fun addTab(tab: CompatTab) { ... tabHost?.newTabSpec(tab.tag)?.run { setIndicator(tab.getText()) // And optional icon ... tabHost?.addTab(this) } } // The other important method, newTab() is part of // the base implementation. }
Java
public class TabHelperEclair extends TabHelper { private TabHost tabHost; ... protected void setUp() { if (tabHost == null) { // Our activity layout for pre-Honeycomb devices // must contain a TabHost. tabHost = (TabHost) mActivity.findViewById( android.R.id.tabhost); tabHost.setup(); } } public void addTab(CompatTab tab) { ... TabSpec spec = tabHost .newTabSpec(tag) .setIndicator(tab.getText()); // And optional icon ... tabHost.addTab(spec); } // The other important method, newTab() is part of // the base implementation. }
您現在有兩種 CompatTab
和 TabHelper
實作方式:一種適用於搭載 Android 3.0 以上版本的裝置並使用新的 API,另一種則適用於搭載 Android 2.0 以上版本的裝置並使用舊版 API。在下一堂課中,我們會探討如何在應用程式中使用這些實作項目。