ActionBar pre Honeycomb

2019-01-11 01:44发布

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

7条回答
Viruses.
2楼-- · 2019-01-11 02:18

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

查看更多
成全新的幸福
3楼-- · 2019-01-11 02:18

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.

查看更多
Rolldiameter
4楼-- · 2019-01-11 02:22

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;
  }
查看更多
甜甜的少女心
5楼-- · 2019-01-11 02:24

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
查看更多
迷人小祖宗
6楼-- · 2019-01-11 02:25

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.

查看更多
聊天终结者
7楼-- · 2019-01-11 02:25

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

查看更多
登录 后发表回答