使用旧版 API 创建实现

本课程讨论如何创建既能反映新版 API 又能支持旧版设备的实现。

决定替代方案

以向后兼容的方式使用新版界面功能最具挑战性的任务是,为旧版平台决定并实现一个旧版(回退)解决方案。在许多情况下,可以使用旧版界面框架功能实现这些新版界面组件的目的。例如

将新版界面组件向后移植到旧版设备通常没有一刀切的解决方案。请注意用户体验:在旧版设备上,用户可能不熟悉新版设计模式和界面组件。考虑如何使用熟悉的元素提供相同的功能。在许多情况下,这并不是一个大问题——如果新版界面组件在应用生态系统中非常突出(例如应用栏),或者交互模型非常简单直观(例如使用 ViewPager 的滑动视图)。

使用旧版 API 实现标签页

要创建旧版应用栏标签页实现,您可以使用 TabWidgetTabHost(尽管也可以使用水平排列的 Button 微件)。在名为 TabHelperEclairCompatTabEclair 的类中实现此功能,因为此实现使用的 API 不晚于 Android 2.0 (Eclair) 引入。

Class diagram for the Eclair implementation of tabs.

图 1. Eclair 标签页实现的类图。

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.
}

您现在有两种 CompatTabTabHelper 的实现:一种在运行 Android 3.0 或更高版本并使用新 API 的设备上工作,另一种在运行 Android 2.0 或更高版本并使用旧 API 的设备上工作。下一课程讨论如何在您的应用中使用这些实现。