Using the Version-Aware Component 使用版本识别组件
来源:互联网 发布:一块红布知乎 编辑:程序博客网 时间:2024/05/08 13:23
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 factory 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 aTabWidget
and 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 TabHelperHoneycomb
implementation, all you need is a FrameLayout
to contain the tab contents, since the tab indicators are provided by theActionBar
:
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.xml
layout to inflate depending on the platform version. This is the same logic shown in the previous section to determine whichTabHelper
implementation to use.
Use TabHelper in Your Activity
In your activity's onCreate()
method, you can obtain aTabHelper
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 aTabHelperHoneycomb
or TabHelperEclair
object. The concrete class that's actually used is opaque to the activity, since they share the commonTabHelper
interface.
Below are two screenshots of this implementation running on an Android 2.3 and Android 4.0 device.
- Using the Version-Aware Component 使用版本识别组件
- 组件使用component
- vue component 组件使用
- attation-----using the version correctly
- 人脸识别 - Pose-Aware Face Recognition in the Wild
- Using the TDockTabSet component by Jeremy North
- 使用谷歌开源组件tesseract-OCR识别身份证(windows版本)
- symfony路由组件(The Routing Component)
- Using External Version Control Systems with Unity【使用第三方版本控制工具】
- #489 – 使用InkCanvas 进行手势识别(Using the InkCanvas to Recognize Gestures)
- 使用Django Component 系列2 :翻页组件
- vue中component组件的props使用
- Android Architecture Component之Lifecycle-Aware Components
- 组件 - Component
- 使用谷歌开源组件tesseract-OCR识别身份证,通过opencv处理图像后再进行识别(windows版本)
- Current version is 2.2.1. If using the gradle wrapper
- 使用 jQuery 识别浏览器版本
- Subversion Version Control: Using The Subversion Version Control System in Development Projects
- 书后思考:输出三个数中的中间数。从小到大输出三个数
- java实现zip压缩文件/文件夹
- Creating an Implementation with Older APIs 用较早版本的APIs实现抽象类
- 不能为虚拟电脑 xxx 打开一个新任务. Failed to load VMMR0.r0 (VERR_SUPLIB_OWNER_NOT_ROOT). 返回 代码:NS_ERROR_FAIL
- iOS 6.x 旋转问题
- Using the Version-Aware Component 使用版本识别组件
- 源程序丢失了一部分,可通过 ILSpy 对 EXE 项目文件反编译
- LocationRecordService-1
- qwt安装和使用(windows和linux)
- 科大讯飞 Mobile Speech Platform
- Oracle UNDO - 自动管理疑问解答
- SQL2000升级到了SQL2008遇到的问题之一
- IOS 5下强大的UI修改工具—— UIAppearance .
- 为什么Fibonacci数列相邻两项之比会趋于0.618?