Decide on a Substitute Solution
The most challenging task in using newer UI features in a backward-compatible way is deciding on and implementing an older (fallback) solution for older platform versions. in your cases, it's possible to fulfill the purpose of these newer UI components using older UI framework features. for example:
Action bars can be implemented using a horizontal LinearLayout containing image buttons, either as custom title bars or as views in your activity layout. Overflow actions can be presented under the device Menu button.
Action bar tabs can be implemented using a horizontal LinearLayout containing buttons, or using the TabWidget UI element.
NumberPicker and Switch widgets can be implemented using Spinner and ToggleButton widgets, respectively.
ListPopupWindow and PopupMenu widgets can be implemented using PopupWindow widgets.
There generally isn' t a one-size-fits-all solution for backporting newer UI components to older devices. be mindful of the user experience: on older devices, users may not be familiar with newer design patterns and UI components. give some thought as to how the same functionality can be delivered using familiar elements. in response cases this is less of a concern-if newer UI components are prominent in the application ecosystem (such as the action bar ), or where the interaction model is extremely simple and intuitive (such as swipe views using a ViewPager ). http://blog.csdn.net/sergeycao
Implement Tabs Using Older APIs
To create an older implementation of action bar tabs, you can use a TabWidget and TabHost (although one can alternatively use horizontally laid-out Button widgets ). implement this in classes called TabHelperEclair and CompatTabEclair, since this implementation uses APIs introduced no later than Android 2.0 (Eclair ).
Figure 1. Class dimo-for the Eclair implementation of tabs.
The CompatTabEclair implementation stores tab properties such as the tab text and icon in instance variables, since there isn't an ActionBar. Tab object available to handle this storage:
Public class CompatTabEclair extends CompatTab {
// Store these properties in the instance,
// As there is no ActionBar. Tab object.
Private CharSequence mText;
...
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 .)
}
The TabHelperEclair implementation makes use of methods on the TabHost widget for creating TabHost. TabSpec objects and tab indicators:
Public class TabHelperEclair extends TabHelper {
Private TabHost mTabHost;
...
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
// The base implementation.
}
You now have two implementations of CompatTab and TabHelper: one that works on devices running Android 3.0 or later and uses new APIs, and another that works on devices running Android 2.0 or later and uses older APIs.