zoukankan      html  css  js  c++  java
  • 一种解决新版本API完全兼容老版本API的方法

    原文:http://android.eoe.cn/topic/android_sdk

    这节课程我们讨论如何创建一个实现类,即能对应新版本的API,又能够保持对老版本API的支持。

    * 寻找一个替代的解决方案*

    为了保持向后兼容,我们需要用老版本平台的UI框架特性来实现新本版的UI框架特性,这是一件非常具有挑战性的任务。在很多情况下,我们是完全可以做到这一点的,请看下边的例子:

      • Action bars_'能够用一个 horizontal''' LinearLayout'''在你Activity Layout中实现,这个LinearLayout可以添加一个自定义的标题或者是Views,加上'''image buttons''',执行的动作可以在设备的'_Menu button* 来显示。
      • Action bar tabs_' 可以用 '''horizontal LinearLayout''' 加上按钮或者用'_TabWidget UI* 来实现。

    一般情况下,我们无法找到一个完美的方案,可以把新UI组件的完全的移植到旧版本的设备上。在这个问题上我们应该多考虑一下用户体验,使用老版本设备的用户可能对新版本的设计模式并不熟悉。因此我们在实现的时候要考虑相同的功能实现尽量用用户熟悉的方式来实现。很多情况下我们不必过于担心这个问题,如果这个新的UI组件在应用程序的环境中设计的比较优秀(比如:* Action Bar_')或者交互模式非常简单和直观的(比如:'_Swip Views* 应用 ViewPager)。

    * 用较早版本API实现Tabs*

    我们可以用TabWidgetTabHost(我们也可以用* horizontally laid-out_' Button部件)来实现'''ActionBar'''标签。因为我们使用了Android 2.0 (Eclair)以下的APIs,所以我们的实现类名字叫做'''TabHelperEclair'''和'_CompatTabEclair* :

    文件:Backward-compatible-ui-classes-eclair.png

    图1:* Eclair* 实现tabs的类图

    实现* CompatTabEclair* 时需要在变量中保存tab的属性,比如:text、icon等。因为我们已经没有现成的ActionBar.Tab可以帮助来处理这些属性了。

    public class CompatTabEclair extends CompatTab {
    // Store these properties in the instance,
    // as there is no ActionBar.Tab object.
    private CharSequence mText;
    ...

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public CompatTab setText(int resId) {
        // Our older implementation simply stores this
        // information in the object instance.
        mText = mActivity.getResources().getText(resId);
        return this;
    }
    
    ...
    // Do the same for other properties (icon, callback, etc.)
    

    }

    实现* TabHelperEclair_'时需要需要用到TabHost部件来创建TabHost.TabSpec对象和'_tab indicators* :

    public class TabHelperEclair extends TabHelper {
    private TabHost mTabHost;
    ...

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    protected void setUp() {
        if (mTabHost == null) {
            // Our activity layout for pre-Honeycomb devices
            // must contain a TabHost.
            mTabHost = (TabHost) mActivity.findViewById(
                    android.R.id.tabhost);
            mTabHost.setup();
        }
    }
    
    public void addTab(CompatTab tab) {
        ...
        TabSpec spec = mTabHost
                .newTabSpec(tag)
                .setIndicator(tab.getText()); // And optional icon
        ...
        mTabHost.addTab(spec);
    }
    
    // The other important method, newTab() is part of
    // the base implementation.
    

    }

    现在我们有* CompatTab_'和'_TabHelper* 的两个实现类了,一个用在Android 3.0或者更新的版本的APIs中,一个用在Android 2.0或更新版本的APIs中,下节课我们将讨论如何在我们的应用中使用这样两个实现类。

  • 相关阅读:
    java-日期转换
    java-Timestamp
    java-判断年份是不是闰年
    Java中Date与String的相互转换
    ORA-01830
    js数组合并
    js清空子节点
    私钥密码
    图片基本样式
    XMLHttpRequest: 网络错误 0x2ee4, 由于出现错误 00002ee4 而导致此项操作无法完成
  • 原文地址:https://www.cnblogs.com/vus520/p/3152772.html
Copyright © 2011-2022 走看看