ActionBar pre Honeycomb

馋奶兔 提交于 2019-11-26 16:05:30

问题


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.


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



回答5:


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;
  }



回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/6646169/actionbar-pre-honeycomb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!