为什么菜单项不显示为动作?

时间:2017-10-12 17:52:28

标签: android xml

这是xml文件:

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

    <item
        android:id="@+id/main_main_prefs"
        android:icon="@drawable/ic_tune_white_24px"
        android:title="@string/menu_settings"
        android:visible="true"
        app:showAsAction="always" />
</menu>

This is the Android studio render   但实际装置就是这种情况:
Actual Device

更新:这是菜单通胀代码。而图片是真实设备中的响应。

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        (new MenuInflater(this)).inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.main_main_prefs:
                Toasty.info(this,"Prefs Was Clicked").show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

the response

2 个答案:

答案 0 :(得分:0)

你必须像这样创建菜单

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;

而不是

(new MenuInflater(this)).inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);

答案 1 :(得分:0)

您可以尝试添加android:orderInCategory。像这样:

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

    <item
        android:id="@+id/main_main_prefs"
        android:icon="@drawable/ic_tune_white_24px"
        android:title="@string/menu_settings"
        android:orderInCategory="100"
        android:visible="true"
        app:showAsAction="always" />
</menu>