天天看點

Actionbar 中解決溢出菜單不顯示的問題

  一般的來說在Actionbar中在條目過多時會顯示三個豎着的小點的菜單,但在實機測試的時候發現并不顯示,上網查找了之後發現問題所在:如果該機器擁有實體的menu鍵則不在右側顯示溢出菜單,而改為按menu來生成。這樣就不利于統一的界面風格。

我們可以改變系統探測實體menu鍵的存在與否來改變這個的顯示。

菜單顯示是根據public boolean hasPermanentMenuKey ()這個方法來判斷的。這個方法是擷取sHasPermanentMenuKey的boolean值。

解決辦法:通過在onCreate()中

try {

ViewConfiguration mconfig = ViewConfiguration.get(this);

       Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");

       if(menuKeyField != null) {

           menuKeyField.setAccessible(true);

           menuKeyField.setBoolean(mconfig, false);

       }

   } catch (Exception ex) {

   }

這樣就大功告成了

參考:http://developer.android.com/reference/android/view/ViewConfiguration.html#hasPermanentMenuKey