2012-07-11 128 views
0

我想在android中製作選項菜單,但它顯示爲一個上下文菜單。這裏是代碼。無法使選項菜單..顯示爲上下文菜單Android

@Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 

    menu.add("Item1"); 
    menu.add("Item2"); 

    return true; 
    } 

誰能幫助我瞭解Ÿ其顯示爲上下文菜單。 感謝

回答

0

試試這個

public static final int ADD_CATEGORY_INDEX1 = Menu.FIRST; 
public static final int ADD_CATEGORY_INDEX2 = Menu.FIRST+1; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 

     menu.add(0, ADD_CATEGORY_INDEX1, 0, "item1"); 
     menu.add(0, ADD_CATEGORY_INDEX2, 0, "item2"); 

    return super.onCreateOptionsMenu(menu); 
} 
+1

爲什麼你調用'super.onCreateOptionsMenu(menu)'兩次? – Sam 2012-07-11 06:23:44

0

如果你想創建一個菜單,做這樣的事情:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = this.getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return true; 
} 

它顯示了作爲一個上下文菜單,因爲你沒有一個佈局設置爲您的菜單,而不是誇大它。使用提供的代碼,以及類似的XML代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Menu button - Home --> 
<item 
    android:id="@+id/homeButton" 
    android:icon="@drawable/homeicon" 
    android:title="@string/homeButton" 
    android:textSize="10sp" 
    /> 
<!-- Menu button - Location --> 
<item 
    android:id="@+id/locationButton" 
    android:icon="@drawable/locbutton" 
    android:title="@string/locationButton" 
    android:textSize="10sp" 
    /> 
<!-- Menu button - Dining --> 
    <item 
    android:id="@+id/diningButton" 
    android:icon="@drawable/diningbutton" 
    android:title="@string/diningButton" 
    android:textSize="10sp" 
    /> 
    <!-- Menu button - Top25 --> 
    <item 
    android:id="@+id/topXXVButton" 
    android:icon="@drawable/topxxv" 
    android:title="@string/topXXVButton" 
    android:textSize="10sp" 
    /> 

這一切都應該讓你去。

希望它有幫助!

+0

我試過這個,但仍然得到一個上下文菜單而不是選項菜單。我需要在2列和1行的網格底部的菜單。 – user1516790 2012-07-11 07:16:41

0

試試這個方法:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.device_menu, menu); 
    setMenuBackground(); 
    return true; 
    } 

    private void setMenuBackground() { 
    getLayoutInflater().setFactory(new LayoutInflater.Factory() { 
     public View onCreateView(final String name,final Context context,final AttributeSet attributeSet) { 

      if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { 
       try { 
        final LayoutInflater f = getLayoutInflater(); 
        final View view = f.createView(name, null, attributeSet); 
        //view.setBackgroundResource(R.drawable.menu_selector); 

        new Handler().post(new Runnable() { 
         public void run() { 
          view.setBackgroundResource(R.drawable.menu_selector);    
         } 
        }); 
        return view; 
       } catch (final Exception e) { 
        Log.e("##Menu##","Could not create a custom view for menu: "+ e.getMessage(), e);                   
       } 
      } 
      return null; 
     } 
    }); 
} 

@Override 
public View onCreateView(String name, Context context, 
     AttributeSet attrs) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menuItem1: 
     //code for first menu item click  
      break; 
     case R.id.menuItem2: 
      //code for second menu item click  
      break; 
    } 
    return true; 
} 

這裏是menu_selector.xml(在繪製文件夾):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/menu_pressed"/> 
    <item android:state_focused="true" android:drawable="@color/menu_focused"/> 
    <item android:drawable="@color/menu_normal"/> 
</selector> 

這裏是device_menu.xml(佈局文件夾):

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@+id/menuItem1" 
     android:icon="@drawable/icon_for_item_1" 
     android:title="Item1" /> 
    <item android:id="@+id/menuItem2" 
     android:icon="@drawable/icon_for_item_2" 
     android:title="Item2" /> 

    </menu> 

而這正是colors.xml(在 「RES」 的文件夾的 「值」 文件夾):

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

      <color name="menu_normal">#ff222222</color> 
      <color name="menu_focused">#ff444444</color> 
      <color name="menu_pressed">#ffcccccc</color> 

     </resources>