<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在做项目时遇到需要使用ActionBar就写下来了方便以后知道这种方法和使用,当然还有其他方法,以后慢慢研究:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在oncreate写如下代码:</span>
final ActionBar ab = getActionBar(); if (ab != null) { ab.setTitle(R.string.group);设置头 ab.setDisplayHomeAsUpEnabled(true); }
重写如下两个方法:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item = menu.add(0, MENU_ITEM_ADD, 1, R.string.add); item.setIcon(android.R.drawable.ic_menu_add); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); item = menu.add(0, MENU_ITEM_SEARCH, 1, R.string.search); item.setIcon(android.R.drawable.ic_menu_search); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int menuId = item.getItemId(); switch (menuId) { case MENU_ITEM_ADD: showUseDialog(); break; case MENU_ITEM_SEARCH: Intent toSearch=new Intent(GroupListActivity.this, GroupSearchActivity.class); startActivity(toSearch); break; case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); }
样子如下:
时间: 2024-10-11 20:46:39