【问题标题】:How to Add android menu overflow icon in my app如何在我的应用程序中添加 android 菜单溢出图标
【发布时间】:2013-08-01 19:24:17
【问题描述】:

如何在 Layout.xml 上手动添加动作溢出按钮,当用户单击该按钮时,除了打开菜单按钮之外,它还应该执行一些其他任务。

【问题讨论】:

  • 不明白你的问题,你想要一个菜单​​按钮来做什么?为什么不能只添加更多菜单项?
  • 当用户点击菜单按钮时,顶部会出现一个包含两个项目的弹出窗口。但我的实际问题是如何将三个点菜单溢出按钮添加到我的应用程序中,即 Layout.XMl 并提供按钮功能。是否有任何内置命令来添加菜单溢出或者我必须添加菜单溢出的图像???

标签: android xml button menu overflow


【解决方案1】:

仅当设备没有硬件菜单按钮并且您的 menu.xml 中有类似这样的项目时,才会出现溢出图标,

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
</menu>

android:showAsAction="never" 将始终将项目添加到选项菜单或溢出(取决于设备)。鉴于上述条件,它是 android 中的内置函数。

【讨论】:

    【解决方案2】:

    在 onCreate() 内部调用这个方法:

       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();
           }
         }
    

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多