Android support action bar not showing overflow me

2019-06-18 18:42发布

My android app supports 2.2 and higher. I'm using the appcompat support library for the action bar, so it should only show if there are things that don't fit. 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

<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

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>

7条回答
干净又极端
2楼-- · 2019-06-18 19:24

General reference

1 - Override onCreateOption and inflate the menu

Note

Doesn't matter if you return true or call 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.

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

3 - set the showAsAction to always or ifRoom

**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.

Finally you will have something like [remember to user a proper namespace for yourapp]

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);
}
查看更多
乱世女痞
3楼-- · 2019-06-18 19:38

If you are using the support library you'll need to declare the showAsAction from your own namespace; which will look like this app:showAsAction. You can refer to eOnOe's answer for this.

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.

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>
查看更多
叼着烟拽天下
4楼-- · 2019-06-18 19:39

Android show overflow menu only if your device doesn't have menu button. Of course you can create hack a do it default for everyone device but it is't recommended solution. And for your every items add android:showAsAction="never" and this say your device show overflow menu if you don't have menu button.

查看更多
爷、活的狠高调
5楼-- · 2019-06-18 19:40
     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> 
查看更多
Anthone
6楼-- · 2019-06-18 19:41

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?

Because the other two are in the action bar. You specified ifRoom, and there were room for two, not three.

查看更多
女痞
7楼-- · 2019-06-18 19:42

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> 
查看更多
登录 后发表回答