Android 操作栏上的下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11376101/
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
Drop Down Menu on Action bar
提问by Rookie
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.
我正在使用带有操作栏的 android 应用程序菜单,我想将下拉菜单放在操作栏中,就像谷歌地图应用程序中的那样。
Can somebody help me? How to achieve this Please point to some easy tutorial that I can follow.
有人可以帮助我吗?如何实现这一点请指出一些我可以遵循的简单教程。
回答by tpbapp
Add this to your activity onCreate() method:
将此添加到您的活动 onCreate() 方法中:
// 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/values/arrays.xml
res/values/arrays.xml
<string-array name="actions">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
Alternatively you could create your own adapter and layout extended from SpinnerAdapter to display more advanced or dynamic list items.
或者,您可以创建自己的适配器和从 SpinnerAdapter 扩展的布局,以显示更高级或动态的列表项。
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.
为了使 Activity onCreate 代码更加简洁,您还可以更改您的 Activity 以实现 OnNavigationListener 并使用回调代码添加覆盖 onNavigationItemSelected。然后在 setListNavigationCallbacks() 方法中将“callback”更改为“this”。
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.
请注意,您需要为操作栏定位 API 11+,否则您将需要添加版本检查或支持库。
回答by SAndroidD
check this Link also it is helpful example Example link
检查此链接也是有用的示例示例链接
GoogleMap map;
TextView txt;
String[] mapTypes={"Normal","Hybrid","Satellite","Terrain"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
txt=(TextView)findViewById(R.id.textView1);
//to set map Type
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//To set the marker on map on specific location using lat lag
// latitude and longitude
double latitude = 18.520430300000000000;
double longitude = 73.856743699999920000;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Location");
// change color to the marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
// adding marker
map.addMarker(marker);
// Create an array adapter to populate dropdownlist
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, mapTypes);
/** Enabling dropdown list navigation for the action bar */
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener=new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(getBaseContext(), "U Select : "+mapTypes[itemPosition], Toast.LENGTH_SHORT).show();
if (mapTypes[itemPosition].equals("Normal")) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
else if (mapTypes[itemPosition].equals("Hybrid")) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
else if (mapTypes[itemPosition].equals("Satellite")) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else if (mapTypes[itemPosition].equals("Terrain")) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
return false;
}
};
// Setting dropdown items and item navigation listener for the actionbar
getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}