Options Menu not displaying on ActionBar

亡梦爱人 提交于 2019-12-10 20:02:41

问题


I wanted to display Menu in ActionBar using ActionBarActivity.

I am getting options menu while clicking options button instead of displaying in the top of action bar.

I need the options menu at top instead of clicking the options menu from 2.3 onwards

My Code :

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setSubtitle("mytest");
    actionBar.setTitle("Testing");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_action, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_item_one:
        Toast.makeText(this, "clicked search", Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

My XML file :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

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

</menu>

回答1:


Try it:

Create XML file on "../res/menu/menu_main.xml"

manu_main.xml

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

    <item
        android:id="@+id/menu_settings"
        android:showAsAction="never"
        android:icon="@android:drawable/ic_menu_settings"
        android:title="@string/menu_settings"/>

</menu>

The option "never" on "showAsAction", important, if you put that the option always show on ActionBar.

Then now we must asociate this option on the MainActivity.java (class whatever you want).

MainActivity.java

public class MainActivity extends 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.activity_main, menu);
        return true;
    }
}

Now that we define the elements of our action bar is knowing how to respond to keystrokes made ​​by the user about them.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_settings:
            Log.i("ActionBar", "Settings!");;
            //open Activity,Fragments or other action
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

If you have morer options on your menu only add item on XML menu_main.xml and on the principal class add other "case".

For Example:

MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_save:
            Log.i("ActionBar", "Save!");;
            //open Activity,Fragments or other action
            return true;
        case R.id.menu_settings:
            Log.i("ActionBar", "Settings!");;
            //open Activity,Fragments or other action
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

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

        <item
            android:id="@+id/menu_settings"
            android:showAsAction="never"
            android:icon="@android:drawable/ic_menu_settings"
            android:title="@string/menu_settings"/>

        <item
            android:id="@+id/menu_save"
            android:showAsAction="never"
            android:icon="@android:drawable/ic_menu_save"
            android:title="@string/menu_save"/>

    </menu>



回答2:


It is possible that this is because your device has an options button that would open the menu, so Android don't draw the launcher icon.

http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/




回答3:


Try with return super.onCreateOptionsMenu(menu); instead of return true in your onCreateOptionsMenu method according to below android document...

http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

As per above android document...

The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above. So, If you want ActionBar in android 2.3 then you have to use Android Support Library.

Edit:

Your menu XML would need to look like this:

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>

<item android:id="@+id/item_menu_ok" 
android:icon="@drawable/ic_action_ok"
android:title="@string/ok" 
yourapp:showAsAction="always"/>

<item android:id="@+id/item_menu_cancel" 
android:icon="@drawable/ic_action_cancel"
android:title="@string/cancel"
yourapp:showAsAction="always"/>

</menu>

Replace yourapp with your app name or any namespace.




回答4:


I'm not 100% sure, but it looks like your menu XML file should have a menu in it. In the menu, add the items. Like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_menu" >

    <item...

    <item...

</menu>

Perhaps this works.

There are also a few other things to remember in general. Android decides by itself what to show in the ActionBar (depending on what fits there and other parameters). What does not fit in the ActionBar goes into the overflow.




回答5:


Do this in your oncreate()

ActionBar actionBar = getActionBar();
actionBar.show();

This should do what you want. Action Bar




回答6:


Ok, let's try another answer. If, for whatever reason, Android decides not to show the items in the action bar, then what your are seeing is indeed the correct behaviour. Items not in the action bar are shown in overflow.

One reason items don't show in the action bar is because they don't have a icon defined.

You do define an icon, as this,

 android:icon="@drawable/ic_launcher"

and I suspect this is the problem. The launcher icon is of different size (larger) than the action bar items (smaller). Try defining the icon in correct size and see if it is displayed.

http://developer.android.com/design/style/iconography.html

Edit: There is an action bar icon pack to get you started so you can test to see if it works. Download it and use eclipse to import some icon..



来源:https://stackoverflow.com/questions/23607165/options-menu-not-displaying-on-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!