public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注意要改版本 ActionBar actionBar = getActionBar(); // 設置是否將圖標轉換爲可點擊的按鈕,並加一個箭頭圖標,系統默認的圖標 // actionBar.setDisplayHomeAsUpEnabled(true);//這一句就能夠實現圖片和點擊 actionBar.setHomeButtonEnabled(true);// 轉爲可點擊 // 不用系統默認的,本身給圖標,要API 14版本 actionBar.setIcon(R.drawable.ie_arrow_normal); actionBar.setTitle("返回"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem item = menu.findItem(R.id.action_more); ImageView imageView = (ImageView) item.getActionView(); imageView.setImageResource(R.drawable.skin_aio_more_nor); // 點擊這個圖片按鈕的時候給一個監聽事件 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 點擊中彈出一個PopupMenu PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);// v裏面的每一項視圖 popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, "添加"); popupMenu.getMenu().add(Menu.NONE, 2, Menu.NONE, "收藏"); popupMenu.getMenu().add(Menu.NONE, 3, Menu.NONE, "返回"); popupMenu .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(MainActivity.this, "添加", 100) .show(); break; case 2: Toast.makeText(MainActivity.this, "分享", 100) .show(); break; case 3: Toast.makeText(MainActivity.this, "返回", 100) .show(); break; } return false; } }); popupMenu.show(); } }); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: Toast.makeText(MainActivity.this, "添加", Toast.LENGTH_SHORT).show(); break; case R.id.action_camera: Toast.makeText(MainActivity.this, "照相", Toast.LENGTH_SHORT).show(); break; case R.id.action_call: Toast.makeText(MainActivity.this, "打電話", Toast.LENGTH_SHORT).show(); break; case android.R.id.home:// ActionBar的返回圖標 Intent intent = new Intent(this, MainActivity.class); intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);// 將上面的頁面清除掉,跟SingleTaskTop差很少的效果 startActivity(intent); break; } return super.onOptionsItemSelected(item); } } 文件夾下的menu/菜單選項 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_add" android:icon="@android:drawable/ic_menu_add" android:orderInCategory="1" android:showAsAction="always" android:title=""/> <item android:id="@+id/action_camera" android:icon="@android:drawable/ic_menu_camera" android:orderInCategory="1" android:showAsAction="always" android:title=""/> <item android:id="@+id/action_call" android:icon="@android:drawable/ic_menu_call" android:orderInCategory="1" android:showAsAction="always" android:title=""/> <item android:id="@+id/action_more" android:actionViewClass="android.widget.ImageView" android:orderInCategory="1" android:showAsAction="always" android:title=""/> </menu> // // 設置導航模式,在另外一Activity實現 // actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);// 下拉列表模式 // List<Map<String, String>> pullList = new ArrayList<Map<String, // String>>(); // Map<String, String> option1 = new HashMap<String, String>(); // option1.put("option", "首頁"); // Map<String, String> option2 = new HashMap<String, String>(); // option2.put("option", "下頁"); // Map<String, String> option3 = new HashMap<String, String>(); // option3.put("option", "上頁"); // pullList.add(option1); // pullList.add(option2); // pullList.add(option3); // SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, // pullList, android.R.layout.simple_list_item_1, // new String[] { "option" }, new int[] { android.R.id.text1 }); // actionBar.setListNavigationCallbacks(adapter, // new OnNavigationListener() { // // @Override // public boolean onNavigationItemSelected(int itemPosition, // long itemId) { // Toast.makeText(MainActivity.this, // "你點中了第" + itemPosition+1 + "項", // Toast.LENGTH_SHORT).show(); // return false; // } // });