简体   繁体   English

Android ActionBar compat溢出菜单未显示在sdk 10上

[英]Android ActionBar compat overflow menu not showing on sdk 10

Hello and thank you for the time you take in reading this question. 您好,感谢您抽出时间阅读此问题。

I am trying to develop an android app which will use the ActionBar compat library. 我正在尝试开发一个将使用ActionBar compat库的Android应用程序。 I have followed (as far as I see it) all the recommendations when using the compat library. 在使用compat库时,我遵循了(据我所知)所有建议。 My Manifest looks like this(only relevant code shown): 我的清单看起来像这样(只显示相关代码):

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"        
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application            
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >

    </application>

</manifest>

As you can see I am targeting sdk 8+. 如你所见,我的目标是sdk 8+。 I have used the Theme.AppCompat theme as recommended. 我已经按照建议使用了Theme.AppCompat主题。

My menu file looks like this: 我的菜单文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cds="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_map"
        android:icon="@drawable/ic_action_map"
        android:title="@string/action_map"
        cds:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        cds:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_mail"
        android:icon="@drawable/ic_action_mail"
        android:title="@string/action_mail"
        cds:showAsAction="ifRoom"/>

</menu>

I am using my own namespace for the showAsAction attribute. 我使用自己的命名空间作为showAsAction属性。

My activity extends the ActionBarActivity class. 我的活动扩展了ActionBarActivity类。

The problem is this: On sdk 10 (android 2.3.3), both on device and emulator, the overflow menu (the three dots on the right side of the action bar) are not shown. 问题是:在设备和模拟器上的sdk 10(android 2.3.3)上,溢出菜单(操作栏右侧的三个点)未显示。 Only the first 2 of the menu items are shown on the action bar. 操作栏上仅显示前两个菜单项。 If i press the "Menu" button on the device then the third item is shown from the bottom left corner of the screen (not from the upper right corner as on the devices with more recent android versions). 如果我按下设备上的“菜单”按钮,则第三项显示在屏幕的左下角(而不是从右上角显示,因为在具有更新的Android版本的设备上)。 The same code works well on android sdk 17 on emulator (the overflow menu is shown with the proper actions). 相同的代码在模拟器上的android sdk 17上运行良好(溢出菜单显示正确的操作)。

I have searched the web for a solution but I could not find one with this specific problem. 我在网上搜索了一个解决方案,但我找不到具有这个特定问题的解决方案。 I would have abandoned the issue if I wouldn't have installed apps on the android 2.3.3 device that have the same action bar and which show the overflow menu icon and work properly like on any recent android device. 如果我没有在Android 2.3.3设备上安装具有相同操作栏并且显示溢出菜单图标并且在任何最近的Android设备上正常工作的应用程序,我会放弃该问题。 One example of this app is the todoist app ( https://en.todoist.com/android ) or the handcent app( https://play.google.com/store/apps/details?id=com.handcent.nextsms&hl=en ) which both behave well on this device. 这个应用程序的一个例子是todoist应用程序( https://en.todoist.com/android )或移动应用程序( https://play.google.com/store/apps/details?id=com.handcent.nextsms&hl = en )这两个都在这个设备上表现良好。

Is there anything I am missing or is there an alternative solution to the recommended way of using the actionbar compat? 我有什么遗漏或是否有使用actionbar compat的推荐方法的替代解决方案?

Thank you for your time. 感谢您的时间。

@Andrei Google have disabled the menu overflow button in appcompat on pre honycomb. @Andrei Google已禁用pre honycomb中appcompat中的菜单溢出按钮。 If You really want to add it go to the android's github repository and download platform_frameworks_support. 如果你真的想添加它,请转到android的github存储库并下载platform_frameworks_support。 It contains sorce for appcompat in platform_framework_support_master/v7/appcompat. 它包含platform_framework_support_master / v7 / appcompat中appcompat的含义。

Create a libs folder inside appcompat and put latest android-support-v4.jar. 在appcompat中创建一个libs文件夹并放入最新的android-support-v4.jar。 Now open file v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java . 现在打开文件v7/appcompat/src/android/support/v7/internal/view/ActionBarPolicy.java You will see that showOverflowMenuButton is returned false for pre honycomb.Just return it true and add this edited appcompat as library to your project and you will not need any custom overflow button This worked with me. 您将看到showOverflowMenuButton对于pre honycomb返回false。请返回true并将此已编辑的appcompat作为库添加到您的项目中,您将不需要任何自定义溢出按钮这对我有用。 Sorry for my English 对不起我的英语不好

EDIT: actual code from android/support/v7/internal/view/ActionBarPolicy.java 编辑:来自android/support/v7/internal/view/ActionBarPolicy.java实际代码

public boolean showsOverflowMenuButton() {
    // Only show overflow on HC+ devices
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

Try to show as I did. 试着像我一样表现出来。

I add overflow menu (three dots) manually: 我手动添加溢出菜单(三个点):

<item
    android:id="@+id/menu_more"
    android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_light"
    android:title="@string/action_settings"
    myapp:showAsAction="always">
    <menu>
        <item
            android:id="@+id/submenu_about"
            android:showAsAction="always"
            android:title="@string/menu_about"/>
    </menu>
</item>

and override menu button click in activity to show this menu (solution from Opening submenu in action bar on Hardware menu button click ): 和覆盖菜单按钮单击活动以显示此菜单(解决方案从硬件菜单按钮上的操作栏中的打开子菜单单击 ):

private Menu mainMenu;    

...

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:

                mainMenu.performIdentifierAction(R.id.menu_more, 0);
                return true;
            }

        }
        return super.onKeyUp(keyCode, event);
    }

Result on 2.2 looks like: 2.2的结果如下:

Android 2.2溢出菜单

Hope it helps you. 希望它能帮到你。

I found the answer finally for this situation. 我终于找到了这个问题的答案。

All you need to do is call the following in the OnCreate in your activity 您只需在活动中的OnCreate中调用以下内容即可

ActionBarPolicy.get(this).showsOverflowMenuButton();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM