2012-10-19 232 views
2

我在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(); 
    } 
+1

你能張貼你的菜單代碼?你讀過[這整個頁面](http://developer.android.com/guide/topics/ui/menus.html)? – dokkaebi

+0

好的,起初我誤解了。你正試圖在主菜單右邊添加一個[SubMenu](http://developer.android.com/reference/android/view/SubMenu.html)? – dokkaebi

回答

0

您可以找到樣本項目,其中已經在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示例中提取的。希望這一個幫助。

0

您可以通過編程方式在您的Activity中添加一個子菜單,如下所示。從教程here改編:

@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中,你可以做這樣的(從here拍攝):

<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" /> 

相關問題