78

在这里我尝试制​​作选项菜单,但菜单没有显示在屏幕上,所以请指导我我在哪里做错了......

MenuTest.java

public class MenuTest extends Activity {
   @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.more_tab_menu, menu);
    return super.onCreateOptionsMenu(menu);

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.feeds:
        break;
    case R.id.friends:
        break;
    case R.id.about:
        break;
    }
    return true;
}
}

我的 XML 文件是 more_tab_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/feeds"
    android:title="Feeds"/>
<item
    android:id="@+id/friends"
    android:title="Friends"/>
<item
    android:id="@+id/about"
    android:title="About"/>
</menu>

请指导我,

4

11 回答 11

85
public class MenuTest extends Activity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.more_tab_menu, menu);

        // return true so that the menu pop up is opened
        return true; 
    }
}

并且不要忘记按模拟器或设备上的菜单按钮或图标

于 2011-06-22T11:57:42.880 回答
32

请看:==

private int group1Id = 1;

int homeId = Menu.FIRST;
int profileId = Menu.FIRST +1;
int searchId = Menu.FIRST +2;
int dealsId = Menu.FIRST +3;
int helpId = Menu.FIRST +4;
int contactusId = Menu.FIRST +5;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu);
    menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu);
    menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu);
    menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu);
    menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu);
    menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu);

    return super.onCreateOptionsMenu(menu); 
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
        // write your code here
        Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG);
        msg.show();
        return true;

    case 2:
        // write your code here
        return true;

    case 3:
        // write your code here
        return true;

    case 4:
        // write your code here
        return true;

    case 5:
        // write your code here
        return true;

    case 6:
        // write your code here
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}
于 2011-06-22T12:15:20.547 回答
12

将您的方法更改onCreateOptionsMenu为 return true。引用文档

您必须返回 true 才能显示菜单;如果您返回 false 则不会显示。

于 2011-06-22T11:37:37.517 回答
10
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.folderview_options, menu);
    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == R.id.locationListRefreshLocations) {
        Cursor temp = helper.getEmployee(active_employeeId);
        String[] matches = new String[1];
        if (temp.moveToFirst()) {
            matches[0] = helper.getEmployerID(temp);
        }
        temp.close();               
        startRosterReceiveBackgroundTask(matches);
    } else if (item.getItemId()==R.id.locationListPrefs) {
        startActivity(new Intent(this, PreferencesUnlockScreen.class));
        return true;
    }           
    return super.onOptionsItemSelected(item);
}   
于 2013-05-03T10:58:12.170 回答
8

您可以创建如下选项菜单:

菜单 XML 代码:

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

    <item
        android:id="@+id/Menu_AboutUs"
        android:icon="@drawable/ic_about_us_over_black"
        android:title="About US"/>
    <item
        android:id="@+id/Menu_LogOutMenu"
        android:icon="@drawable/ic_arrow_forward_black"
        android:title="Logout"/>
</menu>

如何从 MENU XML 获取菜单(将菜单 XML 转换为 java):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_options_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

如何从菜单中获取所选项目:

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

            case R.id.Menu_AboutUs:
                //About US
                break;

            case R.id.Menu_LogOutMenu:
                //Do Logout
                break;
        }
        return super.onOptionsItemSelected(item);
    }
于 2019-09-06T15:19:16.073 回答
3
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class AndroidWalkthroughApp2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // show menu when menu button is pressed
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // display a message when a button was pressed
        String message = "";
        if (item.getItemId() == R.id.option1) {
            message = "You selected option 1!";
        }
        else if (item.getItemId() == R.id.option2) {
            message = "You selected option 2!";
        }
        else {
            message = "Why would you select that!?";
        }

        // show message via toast
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
        toast.show();

        return true;
    }
}
于 2015-05-17T09:12:57.133 回答
2

替换return super.onCreateOptionsMenu(menu); 返回真;在您的onCreateOptionsMenu方法中这将有所帮助

而且您的活动中还应该有onCreate方法

于 2011-06-22T12:32:41.220 回答
0

前面的答案已经涵盖了android中使用的传统菜单。如果您正在寻找替代方案,它们是您可以使用的另一种选择

https://github.com/AnshulBansal/Android-Pulley-Menu

滑轮菜单是传统菜单的替代品,它允许用户直观地选择活动的任何选项。通过向下拖动屏幕来显示菜单,并且在该手势中用户还可以选择任何选项。

于 2014-01-16T11:26:18.717 回答
0

Android UI 编程有点棘手。要启用 Options 菜单,除了您编写的代码之外,我们还需要在您的重写方法 OnCreate() 中调用 setHasOptionsMenu(true)。希望这会帮助你。

于 2014-09-09T14:19:02.340 回答
0

如果您的设备运行的是 Android v.4.1.2 或更低版本,
则菜单不会显示在操作栏中。
但可以通过菜单-(硬件)-按钮访问。

于 2015-11-25T09:05:00.573 回答
0

美好的一天我被检查了如果你选择空Activity 你没有内置菜单功能对于内置你必须选择基本Activity 这样你Activity将运行onCreateOptionsMenu

或者,如果你在 Empty 工作,Activity从开始 Chengestyles.xml

于 2016-10-24T20:53:07.423 回答