下拉菜单中的操作栏 [英] Drop Down Menu on Action bar
问题描述
我的工作与行动酒吧Android应用程序菜单,我想提出的下拉菜单中像一个present在谷歌地图应用程序的操作栏。
I am working on an android application Menu with Action Bar, I want to put the dropdown menu in the action bar like the one present in Google Maps application.
有人可以帮我吗?如何做到这一点请指向一些简单的教程,我可以遵循。
Can somebody help me? How to achieve this Please point to some easy tutorial that I can follow.
推荐答案
添加到您的活动的onCreate()方法:
Add this to your activity onCreate() method:
// Adapter
SpinnerAdapter adapter =
ArrayAdapter.createFromResource(this, R.array.actions,
android.R.layout.simple_spinner_dropdown_item);
// Callback
OnNavigationListener callback = new OnNavigationListener() {
String[] items = getResources().getStringArray(R.array.actions); // List items from res
@Override
public boolean onNavigationItemSelected(int position, long id) {
// Do stuff when navigation item is selected
Log.d("NavigationItemSelected", items[position]); // Debug
return true;
}
};
// Action Bar
ActionBar actions = getActionBar();
actions.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actions.setDisplayShowTitleEnabled(false);
actions.setListNavigationCallbacks(adapter, callback);
本示例要求列表项的数组的资源:
This example requires an array resource for the list items:
RES /价值/ arrays.xml
res/values/arrays.xml
<string-array name="actions">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
另外,您可以创建自己的适配器和布局从SpinnerAdapter扩展显示更高级的或动态的列表项。
Alternatively you could create your own adapter and layout extended from SpinnerAdapter to display more advanced or dynamic list items.
为使活动的onCreate code甚至更简洁,你也可以改变你的活动来实现OnNavigationListener并添加onNavigationItemSelected的回调code覆盖。然后改变回调到本在setListNavigationCallbacks()方法。
To make the activity onCreate code even neater you could also change your Activity to implement OnNavigationListener and add the override onNavigationItemSelected with the callback code. Then change "callback" to "this" in the setListNavigationCallbacks() method.
请注意,您将需要针对API 11+的行动吧,否则你将需要添加版本检查或支持库。
Please note you will need to target API 11+ for the action bar, otherwise you will need to add version checking or a support library.
这篇关于下拉菜单中的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!