Using the Version-Aware Component 使用版本识别组件
2013-05-02 12:20
363 查看
Now that you have two implementations of
logic for switching between these implementations, creating version-aware layouts, and finally using the backward-compatible UI component. http://blog.csdn.net/sergeycao
factory for creating version-appropriate
res/layout/main.xml:
For the
res/layout-v11/main.xml:
At runtime, Android will decide which version of the
When running the application, this code inflates the correct activity layout and instantiates either a
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
TabHelperand
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
TheTabHelperabstract class acts as a
factory for creating version-appropriate
TabHelperand
CompatTabinstances, 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
TabWidgetand
TabHost, along with a container for tab contents:
res/layout/main.xml:
<!-- This layout is for API level 5-10 only. --> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost>
For the
TabHelperHoneycombimplementation, all you need is a
FrameLayoutto contain the tab contents, since the tab indicators are provided by the
ActionBar:
res/layout-v11/main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" />
At runtime, Android will decide which version of the
main.xmllayout to inflate depending on the platform version. This is the same logic shown in the previous section to determine which
TabHelperimplementation to use.
Use TabHelper in Your Activity
In your activity'sonCreate()method, you can obtain a
TabHelperobject and add tabs with the following code:
@Override public 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
TabHelperHoneycombor
TabHelperEclairobject. The concrete class that's actually used is opaque to the activity, since they share the common
TabHelperinterface.
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).
相关文章推荐
- Using External Version Control Systems with Unity【使用第三方版本控制工具】
- 无法识别工具版本3.5 (the tool version 3.5 is unrecognized)
- 使用谷歌开源组件tesseract-OCR识别身份证,通过opencv处理图像后再进行识别(windows版本)
- #489 – 使用InkCanvas 进行手势识别(Using the InkCanvas to Recognize Gestures)
- jsf in action 笔记:在组件中使用EL语言(Using the EL with components)--2008.04.11
- 使用Common.Logging与log4net的组件版本兼容问题
- CentOS、Redhat中锁定、解锁软件包版本的方法(yum versionlock使用详解)
- 使用游标批量更新/填充数据库(The Using of Cursor)
- 关于在vc6后续版本MFC程序中使用Media ActiveX组件
- 关于系统默认JDK1.8, 但是使用Maven 的mvn -version命令时发现Java版本为其他解决办法
- 网站中使用了Excel组件问题 Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine
- 使用Maven强制控制依赖和依赖组件的版本
- 识别你的ADFS是什么版本的(Which version of ADFS is running)
- Android Studio 使用教程(十五)之 This support library should not use a different version(x)than the compileS
- 如何使用API函数GetFileVersionInfo,获得版本信息
- 使用dataBinding出现Warning:Using incompatible plugins for the annotation processing: android-apt.
- 使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
- Error:Failed to complete Gradle execution. Cause: The version of Gradle you are using (2.14.1) does
- Using CAS without the Login Screen(不使用cas登陆页面)
- #400 – 使用ItemsPanel 属性将WrapPanel 作为ListBox的显示面板(Using a WrapPanel as the Items Panel for a ListBox)