Android中ActionBar及Overflow的顯示

來源:互聯網
上載者:User

標籤:

轉自:http://www.sxt.cn/u/756/blog/4386

 

最近在按照Android的API文檔學習Android中actionbar的使用,Action bar 最基本的形式,就是為 activity 顯示標題,並且在標題左邊顯示一個 app icon。在這樣簡單的形式下,對於所有的 activity 來說,action bar 對告知使用者他們當前所處的位置十分有用,並為你的 app 維護了持續的同一標識。Action Bar是一種新増的導覽列功能,在Android 3.0之後加入到系統的API當中,它標識了使用者當前操作介面的位置,並提供了額外的使用者動作、介面導航等功能。使用ActionBar的好處是,它可以給提供一種全域統一的UI介面。

配置實現ActionBar

首先需要自己配置menu,在menu中添加相對應的Item(/res/menu/main.xml)中進行如下配置:,添加了三個Item

123456789101112131415 <item    android:id="@+id/action_settings"    android:icon="@drawable/ic_launcher"    android:showAsAction="always|withText"    android:title="@string/action_settings"/><item    android:id="@+id/icon_edit"    android:icon="@drawable/ic_launcher"    android:showAsAction="never|withText"    android:title="@string/main_actionEdit"/><item    android:id="@+id/icon_search"    android:icon="@drawable/ic_launcher"    android:showAsAction="never|withText"    android:title="@string/main_actionSearch"/>

item中showAsAction屬性有四個:
  1、always:這個值會使功能表項目一直顯示在Action Bar上。
  2、ifRoom:如果有足夠的空間,這個值會使功能表項目顯示在Action Bar上。
  3、never:這個值使功能表項目永遠都不出現在Action Bar上。
  4、withText:這個值使功能表項目和它的表徵圖,菜單文本一起顯示。

同時還需要在Activity重寫onCreateOptionsMenu方法:

123456 @Override  public boolean onCreateOptionsMenu(Menu menu) {      MenuInflater inflater = getMenuInflater();      inflater.inflate(R.menu.main, menu);      return super.onCreateOptionsMenu(menu);  }

 按照API文檔出現的結果應該是這個樣子的,注意三個點:

 

但是現實是:

 由於我是初學者,所以當官方文檔和個人實戰有衝突的時候,就鬱悶了,完全是一步步按照文檔過來的,所以這個時候就鬱悶了,上網也是一通猛找,發現了原因:

http://developer.android.com/guide/topics/ui/actionbar.html,這個上面有段英文:

The action bar provides users access to the most important action items relating to the app‘s current context. Those that appear directly in the action bar with an icon and/or text are known as action buttons. Actions that can‘t fit in the action bar or aren‘t important enough are hidden in the action overflow. The user can reveal a list of the other actions by pressing the overflow button on the right side (or the device Menu button, if available).

我就不翻譯了,簡單點說如果裝置有菜單功能鍵,顯示隱藏動作的功能將會由Menu菜單功能鍵實現,點擊了一下menu,效果如下:

 

菜單出現了,不過就是沒有那三個點,還是有點不甘心,又找了找,還有有人遇到過這個問題的,對於那三個點菜單的話,Android 3.0 (API level 11) +標準叫法叫做:action overflow button低版本的(Android 2.3.x (API level 10) or lower)對應的叫做:overflow menu,效果(不是三個點)是More按鈕.

如果非要顯示出來Overflow的菜單,需要手動的寫代碼去載入出來,stackoverflow有老外貼了代碼,onCreate中調用一下就可以了,我就不了:

http://stackoverflow.com/questions/20444596/how-to-force-action-bar-overflow-icon-to-show

123456789101112 private void getOverflowMenu() {      try {         ViewConfiguration config = ViewConfiguration.get(this);         Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");         if(menuKeyField != null) {             menuKeyField.setAccessible(true);             menuKeyField.setBoolean(config, false);         }     } catch (Exception e) {         e.printStackTrace();     } }

 

  

隱藏ActionBar,使用getActionBar擷取之後直接隱藏就行,就不貼圖了:

12 ActionBar bar=getActionBar();bar.hide();

這個時候還可以設定Menu中Item的點擊事件:

12345678910111213141516171819 @Override   public boolean onOptionsItemSelected(MenuItem item) {       switch (item.getItemId()) {       case R.id.icon_edit:          Toast.makeText(this, "觸發編輯按鈕事件", Toast.LENGTH_SHORT).show();           return true;       case R.id.action_settings:           Intent intent=new Intent(this,PesonActivity.class);              intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP                        | Intent.FLAG_ACTIVITY_NEW_TASK);             startActivity(intent);           return true;       case R.id.icon_search:           Toast.makeText(this, "觸發搜尋按鈕事件", Toast.LENGTH_SHORT).show();           return true;       default:           return super.onOptionsItemSelected(item);   }   }
 手動實現Menu

Activity之間是可以相互調用的,也可以從子Activity返回到父Activity,建立一個PersonActivity,通過手動寫代碼去配置Menu:

12345678910111213 @Overridepublic boolean onCreateOptionsMenu(Menu menu) {    super.onCreateOptionsMenu(menu);    //添加功能表項目    MenuItem add=menu.add(0,0,0,"添加");    MenuItem del=menu.add(0,0,0,"刪除");    add.setIcon(R.drawable.btn_check_on_pressed);    del.setIcon(R.drawable.btn_close_selected);    //綁定到ActionBar      add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);    del.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);    return true;}

 效果:

這個時候可以看到紅色地方有一個返回的箭頭,需要在Activity中配置一下:

1 android:parentActivityName="com.example.googleaction.MainActivity"

PesonActivity的Activity中需要調用一個方法:

1 getActionBar().setDisplayHomeAsUpEnabled(true);

Android中ActionBar及Overflow的顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.