android fragment向action bar新增選單處理,隱藏等
阿新 • • 發佈:2019-01-27
可以給不同的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方法新增選單影響事件
http://blog.csdn.net/think_soft/article/details/7272864@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); } }
二:隱藏Navigation
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
getActivity().getActionBar().setNavigationMode(ActionBar.DISPLAY_HOME_AS_UP);