简体   繁体   中英

Menu doesn't show up on the actionbar

I'm trying to create a menu for my actvity . But it can not show up on the actionbar like I do. Just help me where wrong.

This is my menu .

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sship.MainActivity" 
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/menu_item_search"
    android:showAsAction="always"/>

<item 
    android:id="@+id/about"
    android:orderInCategory="1"
    android:title="@string/menu_item_about_app"/>

<item 
    android:id="@+id/help"
    android:orderInCategory="2"
    android:title="@string/menu_item_help"/>

This is my activity

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@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);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch(item.getItemId()){
        case R.id.search:
            Toast.makeText(this, "Search button selected", Toast.LENGTH_SHORT).show();
            break;
        case R.id.about:
            Toast.makeText(this, "About button selected", Toast.LENGTH_SHORT).show();
            break;
        case R.id.help:
            Toast.makeText(this, "Help button selected", Toast.LENGTH_SHORT).show();
            break;
    }
    return super.onOptionsItemSelected(item);
}}

And this is result

结果

My guess is that you're using the default toolbar defined by the styles.xml for example Theme.AppCompat.Light.DarkActionBar .

I would recommend that you use a support action bar as it is described by the documentation here .

Hint: The docs tell you to define the NoActionBar theme in the app manifest but you can also set it in the styles.xml so you have more control where to use this action bar.

Pre Android 4.4, device has a hardware menu button,so action overflow menu is activated with the hardware button and thus overflow icon is not shown.

You have to do this forcefully in pre-4.4 devices to show action overflow button. Try below code in your activity's onCreate() method

ViewConfiguration configure = ViewConfiguration.get(context);
Field menuField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuField != null) {
    menuField.setAccessible(true);
    menuField.setBoolean(config, false);
}

您必须检查应用程序主题是否其中没有任何操作。如果您共享主题或styles.xml,这将很有帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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