android实现始终显示overflow菜单的方法

2019-12-10 20:01:35王振洲

易采站长站为您分析android实现始终显示overflow菜单的方法,需要的朋友可以参考下

在Android程序设计中,通常来说在Actionbar中在条目过多时会显示三个竖着的小点的菜单,但在实机测试的时候发现并不显示,查找资料并测试之后发现问题所在:如果该机器拥有实体的menu键则不在右侧显示溢出菜单,而改为按menu来生成。这样就不利于统一的界面风格。

我们可以改变系统探测实体menu键的存在与否来改变这个的显示。

菜单显示是根据public boolean hasPermanentMenuKey ()这个方法来判断的。这个方法是获取sHasPermanentMenuKey的boolean值。

解决方法如下:

在onCreate()中加入:

 

  1. try {  ViewConfiguration mconfig = ViewConfiguration.get(this); 
  2. Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");  if(menuKeyField != null) { 
  3. menuKeyField.setAccessible(true);  menuKeyField.setBoolean(mconfig, false); 
  4. }  } catch (Exception ex) {