博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using the Version-Aware Component 使用版本识别组件
阅读量:4047 次
发布时间:2019-05-24

本文共 3099 字,大约阅读时间需要 10 分钟。

Now that you have two implementations of TabHelper and CompatTab—one for Android 3.0 and later and one for earlier versions of the platform—it's time to do something with these implementations. This lesson discusses creating the logic for switching between these implementations, creating version-aware layouts, and finally using the backward-compatible UI component. http://blog.csdn.net/sergeycao

Add the Switching Logic

The TabHelper abstract class acts as a for creating version-appropriate TabHelper and CompatTab instances, based on the current device's platform version:

public abstract class TabHelper {    ...    // Usage is TabHelper.createInstance(activity)    public static TabHelper createInstance(FragmentActivity activity) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {            return new TabHelperHoneycomb(activity);        } else {            return new TabHelperEclair(activity);        }    }    // Usage is mTabHelper.newTab("tag")    public CompatTab newTab(String tag) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {            return new CompatTabHoneycomb(mActivity, tag);        } else {            return new CompatTabEclair(mActivity, tag);        }    }    ...}

Create a Version-Aware Activity Layout

The next step is to provide layouts for your activity that can support the two tab implementations. For the older implementation (TabHelperEclair), you need to ensure that your activity layout contains a and , along with a container for tab contents:

res/layout/main.xml:

For the TabHelperHoneycomb implementation, all you need is a to contain the tab contents, since the tab indicators are provided by the :

res/layout-v11/main.xml:

At runtime, Android will decide which version of the main.xml layout to inflate depending on the platform version. This is the same logic shown in the previous section to determine which TabHelper implementation to use.

Use TabHelper in Your Activity

In your activity's method, you can obtain a TabHelper object and add tabs with the following code:

@Overridepublic void onCreate(Bundle savedInstanceState) {    setContentView(R.layout.main);    TabHelper tabHelper = TabHelper.createInstance(this);    tabHelper.setUp();    CompatTab photosTab = tabHelper            .newTab("photos")            .setText(R.string.tab_photos);    tabHelper.addTab(photosTab);    CompatTab videosTab = tabHelper            .newTab("videos")            .setText(R.string.tab_videos);    tabHelper.addTab(videosTab);}

When running the application, this code inflates the correct activity layout and instantiates either a TabHelperHoneycomb or TabHelperEclair object. The concrete class that's actually used is opaque to the activity, since they share the common TabHelper interface.

Below are two screenshots of this implementation running on an Android 2.3 and Android 4.0 device.

Figure 1. Example screenshots of backward-compatible tabs running on an Android 2.3 device (using TabHelperEclair) and an Android 4.0 device (using TabHelperHoneycomb).

你可能感兴趣的文章
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>