[英]How to add a menu item to an existing Android app menu?
我是android development
的新手,无法将新的Menu
项目添加到应用程序的菜单中并使其打开并在点击时显示菜单视图布局 。 到目前为止,我只能更改该应用程序的menu.xml文件,这足以创建一个与其他button
并排放置的button
,当您按下Menu
按钮时该按钮会显示出来,但不足以使其链接到任何内容。
我的目标是使按钮结果连接到简单的xml布局或对话页面。 我猜我需要更改Java代码,但是我不确定要完成此操作到底需要什么。 我将不胜感激任何建议。
似乎结束了:“要在活动中使用菜单,您需要使用MenuInflater.inflate()膨胀菜单资源(将XML资源转换为可编程对象)。在以下各节中,您将看到如何膨胀菜单。每种菜单类型的菜单。” 我想我需要知道的是如何将其他物品添加到充气机中。
有人碰巧有我可以插入的原始版本吗?
这是一个现有代码的示例,它打开一个对话框并处理关闭和“更改”按钮:
private void openHelpDialog() {
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.aboutview, null);
TextView tv = (TextView)view.findViewById(R.id.aboutVersionCode);
tv.setText(getVersionName() + " (revision " + getVersionNumber() + ")");
new AlertDialog.Builder(MainActivity.this)
.setTitle(getResources().getString(R.string.application_name) + " " + getResources().getString(R.string.menu_help))
.setIcon(R.drawable.about)
.setView(view)
.setNeutralButton(R.string.menu_changes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
MainActivity.this.openChangesDialog();
}
})
.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
}
private void openChangesDialog() {
LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.changeview, null);
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.changelog_title)
.setIcon(R.drawable.about)
.setView(view)
.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//
}
})
.show();
}
private void openClearDialog() {
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.context_menu_clear_grid_confirmation_title)
.setMessage(R.string.context_menu_clear_grid_confirmation_message)
.setIcon(android.R.drawable.ic_dialog_alert)
.setNegativeButton(R.string.context_menu_clear_grid_negative_button_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//
}
})
.setPositiveButton(R.string.context_menu_clear_grid_positive_button_label, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.kenKenGrid.clearUserValues();
}
})
.show();
}
您可以从Eclipse中已经存在的示例项目中找到许多示例。 这是你在问什么?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case R.id.secure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
return true;
case R.id.insecure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
return true;
case R.id.discoverable:
// Ensure this device is discoverable by others
ensureDiscoverable();
return true;
}
return false;
}
实际上,此代码来自BluetoothChat示例。 希望这对您有所帮助。
您可以在活动中以编程方式添加子菜单,如下所示。 从这里的教程改编而成:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
SubMenu menu4 = menu.addSubMenu(Menu.NONE, MENU4, 4,"Menu No. 4");
menu4.add(GROUP1, SUBMENU1, 1, "SubMenu No. 1");
menu4.add(GROUP1, SUBMENU2, 2, "SubMenu No. 2");
menu4.setGroupCheckable(GROUP1,true,true);
return true;
}
在xml中,您可以这样做(从此处获取 ):
<item android:title="Normal 1" />
<item android:id="@+id/submenu"
android:title="Emotions">
<menu>
<item android:id="@+id/happy"
android:title="Happy"
android:icon="@drawable/stat_happy" />
<item android:id="@+id/neutral"
android:title="Neutral"
android:icon="@drawable/stat_neutral" />
<item android:id="@+id/sad"
android:title="Sad"
android:icon="@drawable/stat_sad" />
</menu>
</item>
<item android:title="Normal 2" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.