我在android development
上很新,在添加一個新的Menu
項目到應用程序的菜單並打開它並點擊時顯示一個菜單視圖佈局時遇到問題。目前爲止,我只能更改該應用的menu.xml文件,這足以創建一個button
,與其他人一起出現,當您按下Menu
按鈕時顯示,但不足以使其鏈接到任何東西。如何將菜單項添加到現有的Android應用程序菜單?
我的目標只是按下按鈕導致它連接到一個簡單的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();
}
你能張貼你的菜單代碼?你讀過[這整個頁面](http://developer.android.com/guide/topics/ui/menus.html)? – dokkaebi
好的,起初我誤解了。你正試圖在主菜單右邊添加一個[SubMenu](http://developer.android.com/reference/android/view/SubMenu.html)? – dokkaebi