1
我有map
活動,我試圖將toggle button
添加到包含地圖類型的設置菜單旁邊的操作欄,但我面臨一個問題:切換按鈕正被添加到設置項目菜單,而不是直接到actionbar
。將開關按鈕添加到操作欄
如何將此切換按鈕添加到設置菜單旁邊的操作欄?
map_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/mapTypeNormal"
android:title="Normal"/>
<item
android:id="@+id/mapTypeSatellite"
android:title="Satellitte"/>
<item
android:id="@+id/mapTypeTerrain"
android:title="Terrain"/>
<item
android:id="@+id/mapTypeHybrid"
android:title="Hybrid"/>
<item
android:id="@+id/mapTypeNone"
android:title="None"/>
<item android:id="@+id/menu_toggle"
android:showAsAction="ifRoom"
android:icon="@drawable/off"
android:title="Share"
/>
</menu>
地圖活動:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
System.out.println("ABC MAP onCreateOptionsMenu was invoked.");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNone:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
break;
}
switch (item.getItemId()) {
case R.id.menu_toggle:
if (birthSort) {
// change your view and sort it by Alphabet
item.setIcon(R.drawable.on);
item.setTitle("On");
birthSort = false;
} else {
// change your view and sort it by Date of Birth
item.setIcon(R.drawable.off);
item.setTitle("Off");
birthSort = true;
}
}
return super.onOptionsItemSelected(item);
}
}上的菜單項
我已將它添加到除-
TheLight