开发者

ActionBar pre Honeycomb

I am writing an app for android (2.1 > 3.1) and I would like to use the familiar practice of using the app Icon in Honeycomb apps to go up to the home activity, however, when I run the activity on earlier, non Honeycomb devices where the Activity.getActionBar(); method does not exist yet, the ap开发者_开发百科p force closes, how can I only run this specified code if the device is running honeycomb?

@Override
protected void onStart() {
    super.onStart();
    ActionBar actionBar = this.getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Thanks for any help and have a great day.


I have written a library for Android which will automatically wrap your pre-3.0 activities with a custom implementation of the action bar design pattern. You can then call getSupportActionBar() which will provide a common interface for both the native and custom implementations, depending on which version of Android your application is running on.

The library also allows you to apply custom styles to both of these action bars through a single theme.

You can find out more information as well as screenshots of sample applications at actionbarsherlock.com.

The library is 100% open source and available at github.com/JakeWharton/ActionBarSherlock.


Android pre-Honeycomb doesn't have an ActionBar, so any method concerning the actionBar will just fail. You should take a look at the code from the Google IO app, which uses an ActionBar both for Honeycomb and pre-Honeycomb.

Put simply, it won't work by itself, you'll have to include your own ActionBar code.


From the API Guide for the Action Bar it says:

the Action Bar Compatibility sample app provides an API layer and action bar layout that allows your app to use some of the ActionBar APIs and also support older versions of Android by replacing the traditional title bar with a custom action bar layout.

You can get this by installing the Android 4.1 (API 16) samples.

Then in Eclipse:

  1. Go to File > New > Project
  2. Android > Android Sample Project
  3. Check Android 4.1
  4. Select ActionBarCompat


Since the actionbar dont exist on pre honeycomb you'll have to make do with something else. One suggestion would be to use johannilssons actionbar library which can be found on github. Direct link: https://github.com/johannilsson/android-actionbar


I like to use GreenDroids action bar (plus they include some other pretty things): http://android.cyrilmottier.com/?p=240


As of Revision 18 the Android Support library contains ActionBar support back to API Level 7. This is now the recommended way to support the ActionBar for all versions of Android from 2.1 up and is significantly easier to use than third party libraries or other hacks.


I think the code is self-explanatory

private static int sdkVersion;
 static 
 {
    try {
      sdkVersion = Integer.parseInt(android.os.Build.VERSION.SDK);
    } catch (Exception ex) {
    }
  }

  /** Device support the froyo (Android 2.2) APIs */
  public static boolean isAndroid22() {
    return sdkVersion >= 8;
  }

  /** Device support the Gingerbread (Android 2.3) APIs */
  public static boolean isAndroid23() {
    return sdkVersion >= 9;
  }

  /** Device supports the Honeycomb (Android 3.0) APIs */
  public static boolean isAndroid30() {
    return sdkVersion >= 11;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜