简体   繁体   English

如何在Android ActionBar Compat上强制溢出菜单?

[英]How to force overflow menu on android actionbar compat?

Android action bar compat Android操作栏兼容
Is it possible? 可能吗? On older devices (pre 3.0) the items that don't fit the action bar are only shown when the menu key is pressed, I want these items to be grouped in the actionbar's overflow menu. 在较旧的设备(3.0之前的版本)上,仅当按下菜单键时才会显示不适合操作栏的项目,我希望将这些项目分组在操作栏的溢出菜单中。

The action overflow menu is only available when there is no hard menu button available on the device. 仅当设备上没有硬菜单按钮可用时,操作溢出菜单才可用。 I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here . 我在“用户界面”>“操作栏”下的“框架主题”中发现了这一点,请在此处查看第3个项目符号。

There is an action bar library written by Jake Wharton called ActionBarSherlock. Jake Wharton有一个动作栏库,名为ActionBarSherlock。 Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this. 也许即使在较旧的设备(包括硬菜单按钮)上,它也可以为您提供动作溢出菜单样式,但是我没有对此进行研究。

Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. 编辑: ActionBarSherlock 4.0(当前为候选版本)具有强制执行操作溢出的内置功能。 If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. 如果您想自己扩展ActionBarCompat示例,则可以查看github来了解Jake如何实现它。 I would suggest just looking into using his library all together, as it is very well done. 我建议只考虑一起使用他的库,因为它做得很好。

If you choose to use Jake's library, look into setting up the Activity theme as @style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices. 如果选择使用Jake的库,请研究将Activity主题设置为@ style / Theme.Sherlock.ForceOverflow以强制在较旧的设备上执行溢出菜单。

Edit2: Using ForceOverflow theme causes issues ( example #1 ) on devices with hardware menu button. 编辑2:使用ForceOverflow主题会导致带有硬件菜单按钮的设备出现问题( 示例1 )。 Thus, Jake Wharton is going to remove ForceOverflow in the future versions. 因此,杰克·沃顿(Jake Wharton)将在将来的版本中删除ForceOverflow

Okay, this is simple but hard to figure out. 好的,这很简单,但是很难弄清楚。

You first need a menu item you want to use as the overflow inflater. 您首先需要一个要用作溢流充气机的菜单项。 Example

<item
        android:id="@+id/a_More"
        android:icon="@drawable/more"
        android:showAsAction="always"
        android:title="More">
        </item>

Once you have your item, add a sub-menu containing your items you want in the overflow menu. 拥有商品后,在溢出菜单中添加一个包含所需商品的子菜单。 Example: 例:

<item
    android:id="@+id/a_More"
    android:icon="@drawable/more"
    android:showAsAction="always"
    android:title="More">
    <menu>
        <item
            android:id="@+id/aM_Home"
            android:icon="@drawable/home"
            android:title="Home"/>
    </menu>
</item>

On click this will inflate other items within. 在单击时,这将使其中的其他项目膨胀。 My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". 我的应用程序使用的是ActionBarSherlock 4.0,因此在此之前,您需要访问“ SplitActionBar”。 (Will still work on default android Actionbar) (仍然可以在默认的Android Actionbar上使用)

Here's how: In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in. Honestly it shouldn't matter if you have the actionbar split or not but I prefer it. 方法如下:在您的AndroidManifest.xml文件中,您需要在需要溢出菜单的活动下添加此代码。老实说,是否拆分操作栏并不重要,但我更喜欢。

android:uiOptions="splitActionBarWhenNarrow"

NOTE: Your item that inflates your overflow menu MUST showAsAction="always" 注意: showAsAction="always"您的溢出菜单膨胀的项目必须showAsAction="always"

Vwola! 哇! you have an overflow menu! 您有一个溢出菜单! Hope I helped you out. 希望我能帮到你。 :) :)

Following LeviRockerSk8er's suggestion I've forced to have a overflow menu in the action bar like this: 按照LeviRockerSk8er的建议,我不得不在操作栏中添加如下所示的溢出菜单:

This is the code for "menu.xml": 这是“ menu.xml”的代码:

<item
    android:id="@+id/web_clasica"
    android:icon="@drawable/ic_action_web_site"
    android:showAsAction="ifRoom"
    android:title="@string/menu_web"
    />
<item
    android:id="@+id/overflow_fijo"
    android:icon="@drawable/ic_action_core_overflow"
    android:showAsAction="always"
    android:title="@string/menu_email"
   >
   <menu>
    <item
    android:id="@+id/email"
    android:icon="@drawable/ic_action_new_email"
    android:showAsAction="ifRoom"
    android:title="@string/menu_email"
   />
    <item
    android:id="@+id/share"
    android:icon="@drawable/ic_action_share"
    android:showAsAction="ifRoom"
    android:title="@string/menu_share"
    />
    <item
    android:id="@+id/about"
    android:showAsAction="ifRoom"
     android:icon="@drawable/ic_action_action_about"
    android:title="@string/menu_about"/>
</menu>

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

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