Android ActionBarCompat - 如何使用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10154476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionBarCompat - how to use it
提问by Fred Rogers
I'm trying to use ActionBarCompat on my own project. I've already opened the sample project (http://developer.android.com/resources/samples/ActionBarCompat/index.html), But I don't know how to implement it on my own.
我正在尝试在我自己的项目中使用 ActionBarCompat。我已经打开了示例项目(http://developer.android.com/resources/samples/ActionBarCompat/index.html),但我不知道如何自己实现它。
I can't find any tutorial of some kind. Should I make this project as a library? Can someone give me some directions, please.
我找不到任何类型的教程。我应该把这个项目作为一个图书馆吗?谁能给我一些方向,请。
回答by Andrés Pachon
As for implementation, just stick to the sample code provided under the MainActivity.java
class. You can find it hereor under <your local android-sdks folder>/samples/android-15/ActionBarCompat/src/com/example/android/actionbarcompat/MainActivity.java
. In general, all you need to do is the following:
至于实现,只需坚持MainActivity.java
类下提供的示例代码即可。您可以在此处或下方找到它<your local android-sdks folder>/samples/android-15/ActionBarCompat/src/com/example/android/actionbarcompat/MainActivity.java
。一般来说,您需要做的就是以下几点:
- Code a menu resource where you declare the items for the action bar (See http://developer.android.com/resources/samples/ActionBarCompat/res/menu/main.html)
- Code an Activity that extends
ActionBarActivity
- Override
onCreateOptionsMenu()
so that it inflates the menu you coded in step #1 - Override
onOptionsItemSelected()
so that you handle the event when the user taps any of the ActionBar items you defined in step #1.
- 编写一个菜单资源,在其中声明操作栏的项目(请参阅http://developer.android.com/resources/samples/ActionBarCompat/res/menu/main.html)
- 编写一个扩展的 Activity
ActionBarActivity
- 覆盖
onCreateOptionsMenu()
以便它扩展您在步骤 1 中编码的菜单 - 覆盖,
onOptionsItemSelected()
以便在用户点击您在步骤 1 中定义的任何 ActionBar 项目时处理该事件。
I think it makes sense to build an Android Library project out of the ActionBarCompat code; then you can just reference it from your custom Android project. Remember it's licensed under the Apache License, Version 2.0.
我认为用 ActionBarCompat 代码构建一个 Android 库项目是有意义的;那么您可以从您的自定义 Android 项目中引用它。请记住,它是根据Apache 许可证,版本 2.0获得许可的。
回答by Matthias Robbers
This answer describes how to use the new ActionBarCompat library (July 2013).
此答案描述了如何使用新的 ActionBarCompat 库(2013 年 7 月)。
In Android Studio, open build.gradle
and add this:
在Android Studio 中,打开build.gradle
并添加:
dependencies {
compile 'com.android.support:appcompat-v7:18.0.+'
}
In Eclipse, create a library projectbased on the code in sdk\extras\android\support\v7\appcompat
and add it to your project.
在Eclipse 中,根据中的代码创建一个库项目sdk\extras\android\support\v7\appcompat
并将其添加到您的项目中。
Activities have to extend ActionBarActivity
.
活动要延长ActionBarActivity
。
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
ActionBar ab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ab = getSupportActionBar();
ab.setTitle("Test");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView =
(SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
}
Themes have to be (or extend) one of the Theme.AppCompat
themes, for example:
主题必须是(或扩展)Theme.AppCompat
主题之一,例如:
<activity android:theme="@style/Theme.AppCompat.Light" ... />
Source: How to add ActionBarCompat to your project, blog post by Gabriele Mariotti
来源:如何将 ActionBarCompat 添加到您的项目,Gabriele Mariotti 的博客文章