问题
i have a problem. I am trying to change the default option menu, different for every fragment. I created an xml file for every fragment and i putted them inside res/menu folder. Now i don't know how to tell android to change the default options menu in every fragment. I putted the fragment inside a pager view. The menu i am talking about is the one on the bottom near the home and back buttons.
this is one of the custom menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.prova.Frase" >
<item
android:id="@+id/one"
android:title="Scatta Foto"/>
<item
android:id="@+id/two"
android:title="Scegli dal Rullino"/>
<item
android:id="@+id/three"
android:title="Prossimo sfondo predefinito"/>
</menu>
回答1:
on every fragment you have to override onCreateOptionsMenu
. Don't forget to call setHasOptionsMenu(true);
(in onCreate
e.g.). Here you can find onCreateOptionsMenu
's documentation
回答2:
You can override the onCreateOptionsMenu in each of the Fragments. Hope this helps
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragMenu, menu);
this.menu = menu;
}
来源:https://stackoverflow.com/questions/27440133/custom-options-menu-for-every-fragment