片段中的操作栏菜单不会出现

时间:2015-01-20 09:50:49

标签: android android-fragments android-actionbar

我正在尝试在片段中创建一个带有一个选项的Action Bar,但它不起作用。 我已经按照互联网教程中指出的所有步骤进行了操作:

  1. 创建“ onCreate ”功能,默认情况下不显示“ sethasoptionmenu(true)”。
  2. 使用“ inflater.inflate(R.menu.menu_fotos,菜单);
  3. 编写功能“ onCreateOptionsMenu
  4. 创建函数“ onOptionsItemSelected
  5. 有了这个,我得到的是一个正常的选项菜单。但我需要的是一个Action Bar菜单! 你能救我吗?

    我在这里复制片段的代码:

        package com.carlesqf.laguerra;
    
        import *.*;
    
        public class FragmentContingutCapitols extends Fragment {   
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setHasOptionsMenu(true);
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v= inflater.inflate(R.layout.activity_contingutcapitols, null);        
                // Getting the bundle object passed from "PantallaContingutCapitols"  
                Bundle b = getArguments();      
                String nomcap=LlistaCapitols.name[b.getInt("position")];
                if (nomcap.contains("1700-1701 Les causes del conflicte:"))
                    v = inflater.inflate(R.layout.capitol1700, null);
                else if ((nomcap.contains("1702 – Primers combats. Itàlia i front del Rin:"))) 
                    v = inflater.inflate(R.layout.capitol1702, null);
    ... 
                return v;
            }   
    
            @Override
            public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
                // Do something that differs the Activity's menu here
                super.onCreateOptionsMenu(menu, inflater);      
                inflater.inflate(R.menu.menu_fotos, menu);  
            }   
    
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.action_settings:
                   // do s.th.
                   return true;
                default:
                    return super.onOptionsItemSelected(item);
                }
            }
        }
    

1 个答案:

答案 0 :(得分:0)

实际上,操作栏菜单是常规选项菜单。 NavigationDrawer是您要找的吗?

相关问题