蜂巢前的 ActionBar
我正在为 android (2.1 > 3.1) 编写一个应用程序,我想使用在 Honeycomb 应用程序中使用应用程序图标的熟悉做法来进入家庭活动,但是,当我在早期的非 Honeycomb 上运行该活动时Activity.getActionBar(); 所在的设备方法尚不存在,应用程序强制关闭,如果设备运行蜂巢,我如何才能仅运行此指定的代码?
@Override
protected void onStart() {
super.onStart();
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
感谢您的帮助,祝您有美好的一天。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我为 Android 编写了一个库,它将使用操作栏设计模式的自定义实现自动包装您的 3.0 之前的活动。然后,您可以调用
getSupportActionBar()
,这将为本机和自定义实现提供通用接口,具体取决于您的应用程序运行的 Android 版本。该库还允许您通过单个主题将自定义样式应用到这两个操作栏。
您可以在 actionbarsherlock.com 上找到更多信息以及示例应用程序的屏幕截图。
该库是 100% 开源的,可从 github.com/JakeWharton/ActionBarSherlock 获取。
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.
Honeycomb 之前的 Android 没有 ActionBar,因此任何涉及 actionBar 的方法都会失败。您应该查看 Google IO 应用中的代码,该代码针对 Honeycomb 和 pre-Honeycomb 使用了 ActionBar 。
简而言之,它本身无法工作,您必须包含自己的 ActionBar 代码。
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.
从 操作栏 的 API 指南中可以看出:
您可以通过安装 Android 4.1 (API 16) 示例来获取此信息。
然后在 Eclipse 中:
From the API Guide for the Action Bar it says:
You can get this by installing the Android 4.1 (API 16) samples.
Then in Eclipse:
由于预蜂窝上不存在操作栏,因此您将不得不使用其他东西。
一种建议是使用 johannilssons actionbar 库,该库可以在 github 上找到。
直接链接:https://github.com/johannilsson/android-actionbar
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
我喜欢使用 GreenDroids 操作栏(另外它们还包括一些其他漂亮的东西): http://android.cyrilmottier .com/?p=240
I like to use GreenDroids action bar (plus they include some other pretty things): http://android.cyrilmottier.com/?p=240
从修订版 18 开始,Android 支持库 包含 ActionBar 支持回到 API 级别7. 现在,这是支持 Android 2.1 及以上所有版本的 ActionBar 的推荐方法,并且比第三方库或其他 hack 更容易使用。
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