`
yidongkaifa
  • 浏览: 4063415 次
文章分类
社区版块
存档分类
最新评论

Using the Version-Aware Component 使用版本识别组件

 
阅读更多

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 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).

分享到:
评论

相关推荐

    KONOPKA SIGNATURE VCL CONTROLS VERSION 6 FOR RAD10.2.1

    Next, select the "Raize Components 6.x (Data-Aware)" package from the Design Packages list and click the Remove button. Again, a message box will be displayed to confirm your request--press OK. Next, ...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    These are SQLDataDrivers that can deliver queries to the server using corresponding drivers of the access to datas. -------------------- 2. Installation Library -------------------- ------------...

    SQLMemTable for Delphi / C++ Builder

    Also, if you are replacing an existing version of SQLMemTable, please remove all files and the package of the prior version before running the new setup program.2) Unpack zip archive containing ...

    EhLib 9.1.024

    EhLib 9.1.024 源码版本,自行编译 EhLib 9.1 Build 9.1.024 Professional Edition. ---------------------------------------------- The Library contains components and classes for Borland Delphi versions...

    ehlib_vcl_src_9_3.26

    These are SQLDataDrivers that can deliver queries to the server using corresponding drivers of the access to datas. -------------------- 2. Installation Library -------------------- ------------...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    These are SQLDataDrivers that can deliver queries to the server using corresponding drivers of the access to datas. -------------------- 2. Installation Library -------------------- ------------...

    Raize Components Full Source 6.2.2 For Delphi 10.2 Tokyo

    Next, select the "Raize Components 6.x (Data-Aware)" package from the Design Packages list and click the Remove button. Again, a message box will be displayed to confirm your request--press OK. Next, ...

    rx第三方控件

    Add splitter support to any application by using the RxSplitter component. TRxSlider and TRxSwitch are slider and switch controls respectively with the ability to change their looks. TRxSlider ...

    RC6.2.3forD10.3

    Next, select the "Raize Components 6.x (Data-Aware)" package from the Design Packages list and click the Remove button. Again, a message box will be displayed to confirm your request--press OK. Next, ...

    SDAC2.45 1.19

    These interfaces support the amount of DBMSfunctionality appropriate to the data store, enabling the data storeto share its data.Using BDE in SQL Server oriented client/server applications has ...

    SQL Server Data Access Components 2.45.22 for d6

    These interfaces support the amount of DBMSfunctionality appropriate to the data store, enabling the data storeto share its data.Using BDE in SQL Server oriented client/server applications has ...

    Diamond.ADO.v2.00.for.Delphi.n.BCB.incl.sources-SSG

    Diamond ADO is a Delphi component library, that provide high-speed performance when working with any OLE DB provider or ODBC data source. Diamond ADO uses ActiveX Data Objects 2.x (ADO) to access data...

    EhLib5.0.13 最新的ehlib源码

    These are SQLDataDrivers that can deliver queries to the server using corresponding drivers of the access to datas. -------------------- Installation Library -------------------- If you have ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    Add splitter support to any application by using the RxSplitter component. TRxSlider and TRxSwitch are slider and switch controls respectively with the ability to change their looks. TRxSlider ...

    VclZip pro v3.10.1

    IMPORTANT: If installing the registered version, please be sure to always re-install/rebuild the components (VCLZip and VCLUnZip) to the component pallette (or rebuild the design time package) so that...

    AlphaControls_14.00_Beta

    A scaling engine of the package has been rewritten and is PDI aware now Different displays with different DPI are supported simultaneously under Win 10 if application compiled under RAD Studio 10.2 ...

    Raize3.0.10.rar

    Next, select the "Raize Components 3.0 (Data-Aware)" package from the Design Packages list and click the Remove button. Again, a message box will be displayed to confirm your request--press OK. ...

    suipack trial

    The lastest version : 6.6, update date: 7 Jan. 2012, see what's new (Newly supports RAD Studio XE2, fully supports Sunisoft Skin Solutions v2) SUIPack is a suite of real WYSIWYG VCL skin components ...

    VCLZip Pro v3.03

    (VCLZip Lite: Version 2.23 April 14th, 2002) (VCLZip Pro: Version 3.03 October 4th, 2003)IMPORTANT: If installing the registered version, please be sure to always re-install/rebuild the components ...

    EurekaLog_7.5.0.0_Enterprise

    18)..Fixed: Possible "Unit XYZ was compiled with a different version of ABC" when using packages 19)..Fixed: FastMM shared MM compatibility 20)..Fixed: Minor bugs in stack tracing (which usually ...

Global site tag (gtag.js) - Google Analytics