0

我的应用程序中有一个名为 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。

4

1 回答 1

1

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

来源: 版本 4.2.0 变更日志

变更日志摘录:

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.ForceOverflow的使用。parent也不是<style>名称为MyTheme.ForceOverflow. 执行上述操作将使溢出菜单为您工作。

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

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而不显示存在更多选项的指示符是个人偏好。我从技术上可行的解决方案的角度来回答这个问题,而不是促进使用这种功能。从用户体验的角度来看它是否可取是另一个主题。

于 2013-03-19T10:25:27.563 回答