8

可能重复:
ActionBarSherlock 和 HoloEverywhere - 强制溢出?

我正在使用 ActionbarSherLock 库在 Gingerbread 之前的设备上显示 Actionbar。

我想要操作栏上的溢出菜单..我搜索了很多,我知道有硬件菜单按钮的设备然后溢出菜单将不会显示..

我还查看了该解决方案的 ActionbarSherlock 示例演示,但我仍然无法获得解决方案..

我已经为操作栏创建了演示,当在没有菜单按钮的设备上进行仿真时,它将显示溢出菜单,但如果设备有菜单按钮,则溢出菜单将不会显示..

以下是我的配置:-

 public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);

//        return true;
        return super.onCreateOptionsMenu(menu);
    }
}

显现 :-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.menu.actionbar_using_lib"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

 <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock"
     >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" 
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

菜单配置:-

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/save"
    android:title="@string/menu_save"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|withText"/>

 <item android:id="@+id/setting"
    android:title="@string/menu_settings"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|withText"/>


  <item android:id="@+id/search"
    android:title="@string/menu_search"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>


   <item android:id="@+id/refresh"
    android:title="@string/menu_refersh"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>

    <item android:id="@+id/edit"
    android:title="@string/menu_edit"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>


     <item android:id="@+id/delete"
    android:title="@string/menu_delete"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>

我想显示溢出菜单..所以请给我建议..提前谢谢..

4

1 回答 1

15

注意:有了这个建议,我建议任何读者使用 ForceOverFlow。这只是列出了使其工作的可能性(而是强制它工作)。各有各的。有些人可能想要它并且也喜欢它。其他人可能不会。

我可能在推测,但也许,这可能对你有用。

您可以认为这是一种hack,但我之前使用它来强制在我的一个应用程序中使用溢出菜单并且它可以工作。

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

此外,MisterSmith的第二个链接有一个解决方案,其中包含我的答案。Commonsware已经提出了一些关于强制使用 OverFlow 菜单的想法:How To Control use of OverFlow Menu in ICS

EDIT: While typing out this suggestion, you added a comment. To respond to that, I would like to point out that Jake Wharton took out .ForceOverFlow themes. I haven't tried it with version 4.2.0, but with a custom theme, it just might work. If you absolutely must use ForceOverFlow, you might have to use an older version. Read my answer here: https://stackoverflow.com/a/13180285/450534. Something might just make it work.

EDIT 2: As pointed out by the OP in a comment, the Demos Sample APK, in fact, does ForceOverFlow the action bar in Action Modes. I have a feeling, after checking the relevant Java files on github, that the answer to that lies perhaps in 3 Java files.

  1. Activity以非常规的方式ActionModes添加: https ://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java (第 53 行)menu items
  2. ABS库中的ActionModeJava文件:https ://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/ActionMode.java
  3. MenuItemJava 文件又是 ABS 库的一部分:https ://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/MenuItem.java
于 2012-11-09T11:51:50.697 回答