Android,如何创建选项菜单

在这里我尝试使选项菜单,但菜单是不显示在屏幕上,所以请指导我在哪里我做错了..。

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>

请指引我,

217433 次浏览

Change your onCreateOptionsMenu method to return true. To quote the docs:

要显示菜单,必须返回 true; 如果返回 false,则不会显示该菜单。

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

and don't forget to press the menu button or icon on Emulator or device

请参阅: = =

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

Replace 返回 super.onCreateOptionsMenu (菜单) ; with 返回真; in your OnCreateOptionsMenu method 这会有帮助的

你也应该在你的活动中使用 OnCreate方法

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

以前的答案已经涵盖了 android 中使用的传统菜单。他们是另一个选择,你可以使用,如果你正在寻找一个替代品

Https://github.com/anshulbansal/android-pulley-menu

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

Android 用户界面编程有点棘手。要启用 Options 菜单,除了您编写的代码之外,我们还需要在覆盖的方法 OnCreate ()中调用 setHasOptionsMenu (true)。 Hope this will help you out.

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

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

再见 我被检查过了 如果您选择空 Activity 没有内置菜单函数 必须选择基本 Activity 这样,您 Activity将运行 onCreateOptionsMenu

或者如果您从一开始就在空 Activity中工作 在 styles.xml的变化

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

菜单 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);
}