弹出菜单:可从操作栏而不是设备的“菜单按钮”访问的项目

时间:2013-03-19 10:03:07

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

我的应用程序中有一个名为ResultListViewActivity的ListView活动,它显示数据库中的内容。列表视图中显示的内容取决于在上一个活动中单击的按钮。因此,如果用户点击"按钮1"在Main Activity中,它将在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。

1 个答案:

答案 0 :(得分: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.

要解决此问题,您需要下载使其运行的旧版本(版本4.1.0)的ABS。您可以在此处根据其发布历史记录获取下载列表: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.ForceOverflow属性中使用parent作为主要主题。也不是名为<style>的其他MyTheme.ForceOverflow。执行上述操作将使“溢出”菜单适合您。

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

android:showAsAction="ifRoom|withText"

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

注意:这是一个黑客,由于显然无法访问多个设备,我个人从未测试过它是否适用于所有存在的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中,而不显示存在更多选项的指示符是个人偏好。我从技术上可行的解决方案的立场回答这个问题,而不是促进使用这样的功能。从UX的角度来看是否可行是另一个主题。

相关问题