简体   繁体   English

动作栏中的三点动作溢出菜单?

[英]Three-dot action overflow menu in actionbar?

在此输入图像描述 I noticed that in my app when I add a menu for new devices that dont have a hardware menu button it adds the three dots to the actionbar. 我注意到在我的应用程序中,当我为没有硬件菜单按钮的新设备添加菜单时,它将三个点添加到操作栏。 However, I see on some apps that you can actually move those three dots to the bottom (on the software navigation) How can this be achieved? 但是,我在一些应用程序上看到,您实际上可以将这三个点移到底部(在软件导航上)如何实现? I am using actionbarsherlock as well if that makes a difference. 我正在使用actionbarsherlock,如果这有所作为。

You can "achieve" this by setting your targetSdk below 14. I say "achieve", because this is bad practice. 你可以通过将targetSdk设置targetSdk低于14来“实现”这个。我说“达到”,因为这是不好的做法。 For devices that have software keys, as long as you're using a theme with the ActionBar, it will display the menu on the ActionBar. 对于具有软件键的设备,只要您使用ActionBar的主题,它就会在ActionBar上显示菜单。 If you're using a theme without the ActionBar (non-Holo), it will display the three dots. 如果你使用没有ActionBar(非Holo)的主题,它将显示三个点。

The three dots are hated. 三个点都很讨厌。
The three dots are evil. 这三个点是邪恶的。
The three dots must. 这三个点必须。 be. 是。 eradicated. 根除。

In short, I'd avoid it. 简而言之,我会避免它。 :) :)

See also: Menu Button of Shame 另请参阅: Shame的菜单按钮

So, turns out it's pretty simple, I recently implemented in my app. 事实证明,这很简单,我最近在我的应用程序中实现。

The items that need to be shown in the overflow menu, nest them under one menu item as follows: 需要在溢出菜单中显示的项目,将它们嵌套在一个菜单项下,如下所示:

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

    <item
        android:id="@+id/empty"
        android:orderInCategory="101"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_overflow">

        <menu>        

            <item
                android:id="@+id/action_settings"
                android:orderInCategory="96"
                android:showAsAction="never"
                android:title="@string/menu_settings"
                android:icon="@drawable/ic_action_settings"/>

            <item
                android:id="@+id/action_share"
                android:orderInCategory="97"
                android:showAsAction="never"
                android:title="@string/menu_share"
                android:icon="@drawable/ic_action_share"/>

            <item
                android:id="@+id/action_rate"
                android:orderInCategory="98"
                android:showAsAction="never"
                android:title="@string/menu_rate"
                android:icon="@drawable/ic_action_important"/>

            <item
                android:id="@+id/action_feedback"
                android:orderInCategory="99"
                android:showAsAction="never"
                android:title="@string/menu_feedback"
                android:icon="@drawable/ic_action_edit"/>

            </menu>         
        </item>
</menu>

Now, edit the main activity file as follows: 现在,编辑主活动文件,如下所示:

package com.example.test;
//all your import statements go here

Menu mainMenu=null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); }

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
mainMenu=menu;
return true; }


//Menu press should open 3 dot menu
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==KeyEvent.KEYCODE_MENU) {
        mainMenu.performIdentifierAction(R.id.empty, 0);
        return true; }
    return super.onKeyDown(keyCode, event); }

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

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