android fragment向action bar添加菜单处理,隐藏等


     可以给不同的fragment添加不同的actionbar


一:覆盖onCreateOptionsMenu方法增加菜单文件
       

@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
	    inflater.inflate(R.menu.main, menu);  	
		super.onCreateOptionsMenu(menu, inflater);		
	}

 

二:必须要在onCreate里边加上setHasOptionsMenu(true)

@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);//添加菜单不调用该方法是没有用的
	}

三:覆盖onOptionsItemSelected方法添加菜单影响事件

  

@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		 switch (item.getItemId()) {  
		    case R.id.xx:  
		        Toast.makeText(getActivity(), "xx", Toast.LENGTH_SHORT).show();  
		        return true;  
		    case R.id.bb:  
		        Toast.makeText(getActivity(), "bb", Toast.LENGTH_SHORT).show();  
		        return true;  
		    case R.id.cc:  
		        Toast.makeText(getActivity(), "cc", Toast.LENGTH_SHORT).show();  
		        return true;  
		    case android.R.id.home:  
		        Toast.makeText(getActivity(), "home", Toast.LENGTH_SHORT).show();  
		        getActivity().finish();  
		        return true;
		    default:  
		        return super.onOptionsItemSelected(item);  
		    }  
	}
http://blog.csdn.net/think_soft/article/details/7272864

http://www.cnblogs.com/SharkBin/p/3559213.html



二:隐藏Navigation

getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
        getActivity().getActionBar().setNavigationMode(ActionBar.DISPLAY_HOME_AS_UP);



你可能感兴趣的:(AndroidUi设计)