简体   繁体   English

Android支持操作栏没有显示溢出菜单

[英]Android support action bar not showing overflow menu

My android app supports 2.2 and higher. 我的Android应用程序支持2.2及更高版本。 I'm using the appcompat support library for the action bar, so it should only show if there are things that don't fit. 我正在使用appcompat支持库作为操作栏,所以它只应显示是否有不适合的东西。 I want my action bar to support the overflow button (the three vertical squares) that reveals a menu with the other items when clicked. 我希望我的操作栏支持溢出按钮(三个垂直方块),在单击时显示菜单和其他项目。

In my menu file, I have three items set up. 在我的菜单文件中,我设置了三个项目。 However on the app I only see two of them, and the overflow button is not showing as well. 但是在应用程序上我只看到其中两个,并且溢出按钮也没有显示。

activity_menu.xml activity_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/checkbox"
        android:title="@string/action_settings"
        sord.ids_connect:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_settings2"
        android:icon="@drawable/checkbox_checked"
        android:title="@string/action_settings"
        sord.ids_connect:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_settings3"
        android:icon="@drawable/ic_launcher"
        android:title="@string/action_settings"
        sord.ids_connect:showAsAction="ifRoom" />  

</menu>

java file java文件

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

public class Activity_Menu extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                super.onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

manifest 表现

    <activity
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
        android:name="sord.ids_connect.Activity_Menu"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_menu" >
    </activity>

General reference 一般参考

1 - Override onCreateOption and inflate the menu 1 - 覆盖onCreateOption并使菜单膨胀

Note 注意

Doesn't matter if you return true or call super 如果你返回true或者叫super,那没关系

@Override

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

2 - Add the namespace to the menu.xml file. 2 - 将命名空间添加到menu.xml文件中。

xmlns:**yourapp**="http://schemas.android.com/apk/res-auto"

3 - set the showAsAction to always or ifRoom 3 - 将showAsAction设置为alwaysifRoom

**yourapp**:showAsAction="ifRoom"

4 - if you are using appcompat make sure that you Activity extends ActionBarActivity , you don't have to change any value of the ActionBar to be able to see you menuOption in the bar. 4 - 如果您正在使用appcompat,请确保您的Activity扩展了ActionBarActivity ,您不必更改ActionBar的任何值,以便能够在栏中看到menuOption。

Finally you will have something like [remember to user a proper namespace for yourapp ] 最后你会有类似的东西[记得为yourapp使用一个合适的命名空间]

main.xml main.xml中

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/ic_action_settings"
    android:title="@string/action_settings"
    yourapp:showAsAction="ifRoom" />

on your Activity 在你的活动上

@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);
    return super.onCreateOptionsMenu(menu);
}

If you are using the support library you'll need to declare the showAsAction from your own namespace; 如果您使用的是支持库,则需要从您自己的命名空间声明showAsAction; which will look like this app:showAsAction . 看起来像这个app:showAsAction You can refer to eOnOe 's answer for this. 你可以参考eOnOe的答案。

Using android:showAsAction won't work with the support library because it uses it's own implementation of the action bar rather than the framework's implementation of it. 使用android:showAsAction将无法使用支持库,因为它使用它自己的操作栏实现而不是框架的实现。

You can also always group items into the icon for example: 您还可以始终将项目分组到图标中,例如:

<item ... 
      android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark">
  <menu>    
   <item .../>
   <item .../>
  </menu>
</item>

Android show overflow menu only if your device doesn't have menu button. 只有当您的设备没有菜单按钮时,Android才会显示溢出菜单。 Of course you can create hack a do it default for everyone device but it is't recommended solution. 当然你可以为每个设备创建hack默认设置,但不建议使用解决方案。 And for your every items add android:showAsAction="never" and this say your device show overflow menu if you don't have menu button. 并为您的每个项目添加android:showAsAction="never" ,如果您没有菜单按钮,这表示您的设备显示溢出菜单。

Im testing on my phone, and when I press the menu button, I see the menu appear on the bottom with only 1 option that says "Settings" 我正在手机上测试,当我按下菜单按钮时,我看到菜单显示在底部,只有一个选项显示“设置”

That is the overflow. 那就是溢出。

also why is only 1 options appearing there when I have 3 items in the menu file? 还有为什么当菜单文件中有3个项目时,只有1个选项出现在那里?

Because the other two are in the action bar. 因为其他两个都在动作栏中。 You specified ifRoom , and there were room for two, not three. 你指定了ifRoom ,并且有两个空间,而不是三个。

You need to menu the menu layout as below 您需要菜单菜单布局如下

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_container"
    android:title="Menu"
    android:showAsAction="never">        
<item
    android:id="@+id/action_settings"
    android:icon="@drawable/checkbox"
    android:title="@string/action_settings"
    sord.ids_connect:showAsAction="ifRoom" />

<item
    android:id="@+id/action_settings2"
    android:icon="@drawable/checkbox_checked"
    android:title="@string/action_settings"
    sord.ids_connect:showAsAction="ifRoom" />

<item
    android:id="@+id/action_settings3"
    android:icon="@drawable/ic_launcher"
    android:title="@string/action_settings"
    sord.ids_connect:showAsAction="ifRoom" />  
</item>

</menu> 
     Change Your menu.xml with the following code.




    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_container"
        android:title="Menu"
       android:showAsAction="always">        
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/checkbox"
        android:title="@string/action_settings"
        android:showAsAction="always"/>

    <item
        android:id="@+id/action_settings2"
        android:icon="@drawable/checkbox_checked"
        android:title="@string/action_settings"
        android:showAsAction="always" />

    <item
        android:id="@+id/action_settings3"
        android:icon="@drawable/ic_launcher"
        android:title="@string/action_settings"
        android:showAsAction="always" />  
    </item>

    </menu> 

I had the same problem. 我有同样的问题。 I solved it by: 我通过以下方式解决了

1) commenting return true; 1)评论return true;

2) uncommenting return super.onKeyUp(keyCode, event); 2)取消注释return super.onKeyUp(keyCode, event);

in public boolean onKeyUp(int keyCode, KeyEvent event) in public boolean onKeyUp(int keyCode, KeyEvent event)

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

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