Using the Version-Aware Component uses the Version recognition Component

Source: Internet
Author: User

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 ){
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 TabWidget 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 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. 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 onCreate () method, you can obtain a TabHelper object 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 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 ).

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.