Custom options menu for every fragment

自古美人都是妖i 提交于 2019-12-23 11:46:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!