Downloading the Support Package


The Support Package is provided as a downloadable package from the Android SDK Manager. To install:

  1. Launch the Android SDK Manager.

    From Eclipse, you can selectWindow>Android SDK Manager. Or, launchSDK Manager.exefrom the<sdk>/directory (on Windows only) orandroidfrom the<sdk>/tools/directory.

  2. Expand the Android Repository, checkAndroid Support packageand clickInstall selected.
  3. Proceed to install the package.

When done, all files (including source code, samples, and the.jarfiles) are saved into the<sdk>/extras/android/support/directory. This directory contains each of the different support libraries, such as the library for API level 4 and up and the library for API level 13 and up, each named with the respective version (such asv4/).

Setting Up a Project to Use a Library


To add one of the libraries to your Android project:

  1. In your Android project, create a directory namedlibsat the root of your project (next tosrc/,res/, etc.)
  2. Locate the JAR file for the library you want to use and copy it into thelibs/directory.

    For example, the library that supports API level 4 and up is located at<sdk>/extras/android/support/v4/android-support-v4.jar.

  3. Add the JAR to your project build path.

    In Eclipse, right-click the JAR file in the Package Explorer, selectBuild Path>Add to Build Path.

Your application is now ready to use the library APIs. All the provided APIs are available in theandroid.supportpackage (for example,android.support.v4).

Tip:To see the library APIs in action, take a look at the sample apps in<sdk>/extras/android/support/<version>/samples/.

Warning:Be certain that you not confuse the standardandroidpackages with those inandroid.supportlibrary. Some code completion tools might get this wrong, especially if you're building against recent versions of the platform. To be safe, keep your build target set to the same version as you have defined for yourandroid:minSdkVersionand double check the import statements for classes that also exist in the support library, such asSimpleCursorAdapter.

Using the v4 Library APIs


The support library for v4 provides access to several classes introduced with Android 3.0 and beyond, plus some updated version of existing classes, and even some APIs that currently don't exist in the Android platform. Some of the most useful and notable classes that have counterparts in the v4 support library are:

  • Fragment
  • FragmentManager
  • FragmentTransaction
  • ListFragment
  • DialogFragment
  • LoaderManager
  • Loader
  • AsyncTaskLoader
  • CursorLoader

For each of the classes above (and others not listed), the APIs work almost exactly the same as the counterparts in the latest Android platform. Thus, you can usually refer to the online documentation for information about the supported APIs. There are some differences, however. Most notably:

  • When creating an activity to use fragments, you must declare your activity to extend theFragmentActivityclass (instead of the traditionalActivityclass).
  • To manage your fragments and loaders, you must use the methodsFragmentActivity.getSupportFragmentManager()andFragmentActivity.getSupportLoaderManager()(instead of thegetFragmentManager()andgetLoaderManager()methods).
  • TheActionBarisnot supportedby the library. However, when creating yourOptions Menu, you can declare which items should be added to the Action Bar when it's available (on Android 3.0 or later). You can do so with theMenuCompat.setShowAsAction()method, for example:
    publicboolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater
    .inflate(R.menu.options, menu);
    MenuCompat.setShowAsAction(menu.findItem(R.id.action_search),1);
    returntrue;
    }

    Also see theAction Bar Compatibilitysample for a demonstration of how to useActionBaron Android 3.0+ and also support action bar functionality on older versions.

Tip:To enable the Holographic theme on devices running Android 3.0 or higher, declare in your manifest file that your application targets API level 11, for example:

<uses-sdkandroid:minSdkVersion="4"android:targetSdkVersion="11"/>

This way, your application automatically receives the Holographic theme and the Action Bar for each activity when running on Android 3.0 and higher.

For more information about how you can optimize your application for the latest Android-powered devices, readOptimizing Apps for Android 3.0.

Reference Docs


The reference documentation for the Support Packages is included as part of the Android online developer documentation:

  • Support Package API 4 Reference
  • Support Package API 13 Reference

Samples


If you want to see some code that uses the support libraries, samples are included with the Support Package, inside each support library directory, for example;<sdk>/extras/android/support/v4/samples/. You can also view these samples as part of the Android online developer documentation:

  • Support API 4 Demos
  • Support API 13 Demos

Additionally, theGoogle I/O Appis a complete application that uses the v4 support library to provide a single APK for both handsets and tablets and also demonstrates some of Android's best practices in Android UI design.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android自定义时间控件不可选择未来时间
  2. Android中线程通讯类Handler
  3. Android(安卓)MediaPlayer与Http Proxy结
  4. android的binder机制研究(C++部分) 分享
  5. Android 开机启动过程
  6. androidの高仿支付宝扫描动画效果
  7. Android Studio Gradle两种更新方式
  8. 关于Android(安卓)O系统短信拦截的流程
  9. android5.0以上对于APP_SWITCH和HOME键的
  10. Android启动模拟器出现:Failed to allocat