复制代码 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } 复制代码 查看MenuInflater.inflate(int,Menu)
复制代码 /** * Inflate a menu hierarchy from the specified XML resource. * * @param menuRes Resource ID for an XML layout resource to load (e.g., <code>R.menu.main_activity</code>) * @param menu The Menu to inflate into. The items and submenus will be added to this Menu. */ public void inflate(int menuRes, Menu menu) { XmlResourceParser parser = null; try { parser = mContext.getResources().getLayout(menuRes); AttributeSet attrs = Xml.asAttributeSet(parser);