一种解决新版本API完全兼容老版本API的方法
2013-06-24 16:38
387 查看
原文:http://android.eoe.cn/topic/android_sdk
这节课程我们讨论如何创建一个实现类,即能对应新版本的API,又能够保持对老版本API的支持。
Action bars_'能够用一个 horizontal''' LinearLayout'''在你Activity Layout中实现,这个LinearLayout可以添加一个自定义的标题或者是Views,加上'''image buttons''',执行的动作可以在设备的'_Menu button* 来显示。
Action bar tabs_' 可以用 '''horizontal LinearLayout''' 加上按钮或者用'_TabWidget UI* 来实现。
NumberPicker_'和'''Switch'''部件可以用'''Spinner'''和'_ToggleButton* 部件分别实现。
ListPopupWindow_'和'''PopupMenu'''部件可以用'_PopupWindow* 部件来实现。
一般情况下,我们无法找到一个完美的方案,可以把新UI组件的完全的移植到旧版本的设备上。在这个问题上我们应该多考虑一下用户体验,使用老版本设备的用户可能对新版本的设计模式并不熟悉。因此我们在实现的时候要考虑相同的功能实现尽量用用户熟悉的方式来实现。很多情况下我们不必过于担心这个问题,如果这个新的UI组件在应用程序的环境中设计的比较优秀(比如:* Action Bar_')或者交互模式非常简单和直观的(比如:'_Swip Views* 应用 ViewPager)。
文件: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;
...
}
实现* TabHelperEclair_'时需要需要用到TabHost部件来创建TabHost.TabSpec对象和'_tab indicators* :
public class TabHelperEclair extends TabHelper {
private TabHost mTabHost;
...
}
现在我们有* CompatTab_'和'_TabHelper* 的两个实现类了,一个用在Android 3.0或者更新的版本的APIs中,一个用在Android 2.0或更新版本的APIs中,下节课我们将讨论如何在我们的应用中使用这样两个实现类。
这节课程我们讨论如何创建一个实现类,即能对应新版本的API,又能够保持对老版本API的支持。
* 寻找一个替代的解决方案*
为了保持向后兼容,我们需要用老版本平台的UI框架特性来实现新本版的UI框架特性,这是一件非常具有挑战性的任务。在很多情况下,我们是完全可以做到这一点的,请看下边的例子:Action bars_'能够用一个 horizontal''' LinearLayout'''在你Activity Layout中实现,这个LinearLayout可以添加一个自定义的标题或者是Views,加上'''image buttons''',执行的动作可以在设备的'_Menu button* 来显示。
Action bar tabs_' 可以用 '''horizontal LinearLayout''' 加上按钮或者用'_TabWidget UI* 来实现。
NumberPicker_'和'''Switch'''部件可以用'''Spinner'''和'_ToggleButton* 部件分别实现。
ListPopupWindow_'和'''PopupMenu'''部件可以用'_PopupWindow* 部件来实现。
一般情况下,我们无法找到一个完美的方案,可以把新UI组件的完全的移植到旧版本的设备上。在这个问题上我们应该多考虑一下用户体验,使用老版本设备的用户可能对新版本的设计模式并不熟悉。因此我们在实现的时候要考虑相同的功能实现尽量用用户熟悉的方式来实现。很多情况下我们不必过于担心这个问题,如果这个新的UI组件在应用程序的环境中设计的比较优秀(比如:* Action Bar_')或者交互模式非常简单和直观的(比如:'_Swip Views* 应用 ViewPager)。
* 用较早版本API实现Tabs*
我们可以用TabWidget和TabHost(我们也可以用* 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 | 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中,下节课我们将讨论如何在我们的应用中使用这样两个实现类。
相关文章推荐
- 一种解决新版本API完全兼容老版本API的方法
- 一种解决新版本API完全兼容老版本API的方法
- adb devices offline的一种解决方法:更新adb版本
- win32 API创建tooltip的版本不匹配问题解决方法
- 迅雷7.9版本缺少wlanapi等dll解决方法
- Css兼容IE8版本的解决方法
- Android5.0失效API统计及解决兼容的方法
- Win7向下兼容性问题的一种解决方法
- maven打包编译的异常 :sun.misc.BASE64Decoder 是 Sun 的专用 API,可能会在未来版本中删除的解决方法
- Selenium 3.x 与firefox不兼容的一种解决方法
- pb10以上版本调用ANSI编码的api会出现乱码的解决方法
- Win7向下兼容性问题的一种解决方法
- opencv1.0版本摄像头驱动不兼容问题的解决方法
- JSONArray 实现兼容老版本API的remove方法
- iOS解决低版本兼容的方法
- 安装EntityFramework,提示“找不到与EntityFramework 6.1.3兼容的EntityFramework.zh-Hans版本。"的解决方法。
- linux (REHL)系统的网络服务与此版本的网络管理器不兼容问题解决方法
- Microsoft ReportViewer 控件类型版本兼容问题及解决方法
- C#调用Excel版本不兼容的解决方法
- 自己做IE各个版本兼容的时候用的解决办法方法,IE6到IE11都可以【亲测】