【问题标题】:Popup menu : items reachable from the actionbar instead of the "menu button" of the device弹出菜单:可从操作栏而不是设备的“菜单按钮”访问的项目
【发布时间】:2013-03-19 18:25:27
【问题描述】:

我的应用程序中有一个名为 ResultListViewActivity 的 ListView 活动,它显示来自数据库的内容。列表视图中显示的内容取决于在前一个活动中单击的按钮。所以如果用户点击Main Activity中的“Button 1”,就会在ResultListViewActivity中显示特定的内容,以此类推。

我想为这个活动添加一个菜单:问题是,这个菜单的项目也应该根据之前点击的按钮而改变。有9个案例,每个案例有不同数量的项目。到目前为止,我有这个代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Bundle bundle = getIntent().getExtras();
    int filterVariable = bundle.getInt("filterVariable");
    switch (filterVariable) 
    {
    case 1:
        getSupportMenuInflater().inflate(R.menu.filter_fastfood, menu);
        break;

    case 2:
        getSupportMenuInflater().inflate(R.menu.filter_restaurants, menu);
        break;

    case 3: 
        getSupportMenuInflater().inflate(R.menu.filter_coffeeshops, menu);
        break;

    case 4:
        getSupportMenuInflater().inflate(R.menu.filter_bars, menu);
        break;

// [... and so on]
    }
    return super.onCreateOptionsMenu(menu);
}

效果很好,对于每种情况,我确实在菜单中有不同的项目。但我不希望通过我设备的“菜单按钮”访问这些项目(这绝对不是本能的,用户也很难猜到)。这些项目很重要,所以我想在右上角有一个按钮,它会打开这个菜单(下拉菜单/弹出菜单)。

你知道这样的事情是怎么做到的吗?我在一些谷歌应用程序中看到了这一点,但找不到显示用于执行此操作的工具的教程。
谢谢!

ps:我在我的应用程序中使用 actionbarsherlock。

【问题讨论】:

    标签: android popup android-actionbar actionbarsherlock android-menu


    【解决方案1】:

    如果您使用的是最新版本 4.2.0 的 ABS,则已删除对 .ForceOverflow 主题的支持。

    来源:Version 4.2.0 Changelog

    变更日志摘录:

    Add SearchView widget for standard search interaction (API 8+ only)
    Fix: ShareActionProvider in the split action bar no longer fills the entire screen.
    Fix: ShareActionProvider now does file I/O on a background thread.
    Fix: Automatically correct ColorDrawable not respecting bounds when used as a stacked background.
    Fix: Ensure fragments collection is present before dispatching events.
    Fix: XML-defined onClick searches the correct context for the declared method.
    Fix: Ensure action mode start/finish callbacks are invoked on the activity for the native action bar.
    Fix: Allow tab callbacks to have a fragment transaction instance for any FragmentActivity.
    Fix: Ensure CollapsibleActionView callbacks are dispatched in both native and compatbility action bars.
    Fix: Remove .ForceOverflow themes. These never should have been included.
    

    要解决此问题,您需要下载可以运行的 ABS 的旧版本(版本 4.1.0)。您可以在此处根据其发布历史获取下载列表:http://actionbarsherlock.com/download.html

    这是来自生产应用程序,经过测试和运行。

    您还需要对应用程序的默认主题进行细微更改。例如:

    <style name="MyTheme" parent="@style/Theme.Sherlock.ForceOverflow">
        <item name="android:windowBackground">@color/background</item>
        <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
        <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
    </style>
    
    <style name="MyTheme.ForceOverflow">
        <item name="absForceOverflow">true</item>
    </style>
    

    注意@style/Theme.Sherlock.ForceOverflowparent 属性中用于主主题。也不是名称为MyTheme.ForceOverflow 的附加&lt;style&gt;。执行上述操作将使溢出菜单为您工作。

    您还需要将此属性添加到您的菜单项:

    android:showAsAction="ifRoom|withText"
    

    现在,要强制设备认为它有一个 物理 菜单键,请使用以下代码:

    注意:这是一个 hack,由于明显无法访问多个设备,我个人从未测试过它是否适用于现有的所有 Android 设备。使用风险自负等。仅供参考,我以前用过这个,到目前为止没有任何投诉。

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

    强制溢出菜单出现在ActionBar 中,而不是显示存在更多选项的指示符是个人偏好。我是从技术上可行的解决方案的角度来回答这个问题,而不是促进使用这种功能。从用户体验的角度来看是否可取是另一个主题。

    【讨论】:

    • 哇,谢谢,我有点失望和不安,因为它必须是一个黑客,而且没有更清洁的方法可以做到这一点。当我在 play store 上查看 ActionBarSherlock 演示时,有这个“子菜单”演示完全按照我的意愿做,这暗示着这是一件显而易见的事情。
    • @Phalanx:阅读我的这个答案中的第二个编辑:stackoverflow.com/a/13307583/450534
    • 谢谢,我看了看,但我觉得这将是一个太复杂的路径,因为我还是一个初学者。我将改为探索这种方式:stackoverflow.com/questions/10483612/… 似乎符合我的需求!
    • @Phalanx:听起来可能很复杂,但它确实是一次性的。但无论如何,祝你好运。 :-)
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多