简体   繁体   English

如何在Android中向操作栏显示溢出菜单项

[英]How do I show Overflow Menu Items to Action Bar in Android

I am writing an Android App in which i am trying to show Overflow Menu Items to ActionBar 我正在编写一个Android应用程序,我试图向ActionBar显示溢出菜单项

using this great tutorial link: http://wptrafficanalyzer.in/blog/adding-action-items-and-overflow-menu-items-to-action-bar-in-android/ 使用这个伟大的教程链接: http//wptrafficanalyzer.in/blog/adding-action-items-and-overflow-menu-items-to-action-bar-in-android/

Problem: 问题:

Not getting Overflow Menu Items (Icon)

Please see below Screen Shot for more clarity: 请参阅下面的屏幕截图以获得更清晰: 在此输入图像描述

Manifest.xml: 的Manifest.xml:

<uses-sdk android:minSdkVersion="14" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:uiOptions="splitActionBarWhenNarrow"              
        >

items.xml: items.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/phone"
    android:title="@string/phone"
    android:icon="@drawable/phone"
    android:showAsAction="ifRoom|withText"
/>

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

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

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

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

<item
    android:id="@+id/email"
    android:title="@string/email"
    android:icon="@drawable/email"
    android:showAsAction="ifRoom|withText"
/>
 </menu>

I am using this tutorial, and trying to make Figure 6 : Action items and Overflow menu in Split Action Bar 我正在使用本教程,并尝试在Split Action Bar中创建图6:Action项和Overflow菜单

Please help me to show Overflow Menu Items (ICON) to ActionBar 请帮我向ActionBar显示溢出菜单项(ICON)

Now whenever i do click on Menu Button in emulator, then i am getting rest Menu Items .... 现在每当我点击模拟器中的菜单按钮,然后我得到休息菜单项 ....

Just to say that If your device has a menu-button, the overflow-icon won't show, on the newer phones the overflow button will show. 只是说,如果您的设备有一个菜单按钮,溢出图标将不会显示,在较新的手机上将显示溢出按钮。 I would not reccomend ASMUIRTI's answer since it is an awful hack that breaks consistency with the rest of the apps on the platform. 我不会推荐ASMUIRTI的回答,因为这是一个非常糟糕的黑客攻击,它破坏了平台上其他应用程序的一致性。

you must use 你必须使用

android:showAsAction="never"

and let the android device decide if the overflow menu is needed for that device. 让android设备决定该设备是否需要溢出菜单。

To show three dot icon, to Action Bar, just use below method in your OnCreate(): 要向Action Bar显示三个点图标,只需在OnCreate()中使用以下方法:

   private void getOverflowMenu() {

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

This could be another work around, which really helped me. 这可能是另一项工作,这真的帮助了我。 Keep one drawable with three dots and give it as a menu item. 保留一个可绘制的三个点,并将其作为菜单项。

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


<item
    android:id="@+id/saveDetails"
    android:showAsAction="always"
    android:title="@string/save"/>

<item
    android:id="@+id/menu_overflow"
    android:icon="@drawable/dts"
    android:orderInCategory="11111"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/contacts"
            android:showAsAction="never"
            android:title="Contacts"/>


        <item
            android:id="@+id/service_Tasks"
            android:showAsAction="never"
            android:title="Service Tasks"/>


        <item
            android:id="@+id/charge_summary"
            android:showAsAction="never"
            android:title="Charge Summary"/>


        <item
            android:id="@+id/capture_signature"
            android:showAsAction="never"
            android:title="Capture Signature"/>
    </menu>
</item>

 </menu>

If I understand your question correctly and you want to show all your icons on the action bar change this parameters in your menu items 如果我正确理解了您的问题,并且您想要在操作栏上显示所有图标,请在菜单项中更改此参数

android:showAsAction="ifRoom|withText"

to this 对此

android:showAsAction="always"

如果您使用支持库,为了在操作栏中显示菜单项,您必须使用xml中的以下语法:

yourappname:showAsAction="ifRoom|withText"

The phones which have menu hardware button show extra menu items on click of the hardware button. 具有菜单硬件按钮的电话在点击硬件按钮时显示额外的菜单项。 Newer phones, with no hardware menu button automatically add an Overflow menu icon to the Action Bar. 较新的手机没有硬件菜单按钮会自动将“溢出”菜单图标添加到操作栏。 The extra menu items are those which have "showAsAction" property set to never. 额外的菜单项是“showAsAction”属性设置为never的项目。

Within AndroidManifest.xml between add 在AndroidManifest.xml之间添加

android:theme="@android:style/Theme.Holo.Light"

which adds the action bar in your application. 这会在您的应用程序中添加操作栏。

After that, go to menu.xml and add the followings 之后,转到menu.xml并添加以下内容

xmlns:tools="http://schemas.android.com/tools

between Finally in each item add 最后在每个项目之间添加

android:showAsAction="always"
tools:ignore="AppCompatResource"

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

相关问题 如何在操作栏中显示溢出菜单 - How to show overflow menu in action bar Android操作栏菜单项被强制溢出 - Android action bar menu items getting forced to overflow 如何在操作栏中的溢出菜单中向项目添加图标 - How to add icon to the items in the overflow menu in the action bar 为什么菜单项显示在左下按钮菜单中,而不是操作栏溢出在右上菜单中? - Why menu items show in bottom left button menu instead of action bar overflow top right menu? 如何设置Android操作栏上的菜单项的样式 - How to style the menu items on an Android action bar 如果用户单击Android中的三个点图像,如何显示操作栏溢出菜单 - How to show action bar overflow menu if user clicks three dots image in Android 如何在不使用ActionBarActivity android 2.x版本的情况下显示操作栏溢出菜单支持 - How to show action bar overflow menu support in without using ActionBarActivity android 2.x version 在上下文操作栏中的“溢出”菜单中显示图标 - Show icons in Overflow Menu in Contextual Action Bar android:如何最小化操作栏溢出菜单的宽度 - android: How to minimize width of action bar overflow menu 如何以编程方式打开android支持库操作栏溢出菜单? - How to programatically open the android support library action bar overflow menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM