2011-08-09 80 views
0

在我的三星Galaxy中,當我按下菜單按鈕時,菜單上有一個很深的藍色(幾乎是黑色)背景。我認爲背景色是默認情況下的ROM /版本主題。對於我的應用程序,我需要一個白色(或非常輕)的背景,否則我的圖標幾乎看不見。但我沒有看到改變菜單背景的任何方式。optionsmenu上的白色背景(Android)

我該如何達到這個目標?

在此先感謝

回答

1

在活動課將這個:

protected void setMenuBackground() 
{ 
    getLayoutInflater().setFactory(new Factory() 
    { 
     @Override 
     public View onCreateView (String name, Context context, AttributeSet attrs) 
     { 
      if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) 
      { 
        try 
        { 
          LayoutInflater f = getLayoutInflater(); 
          final View view = f.createView(name, null, attrs); 
          new Handler().post(new Runnable() 
          { 
           public void run() 
           { 
            // Changes the color of the menu item here 
            view.setBackgroundColor(Color.WHITE); 
           } 
          }); 
          return view; 
        } 
        catch (InflateException e) 
        { 
        } 
        catch (ClassNotFoundException e) 
        { 
        } 
      } 
      return null; 
     } 
    }); 
} 

然後創建onCreateOptionsMenu()監聽器:

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    super.onCreateOptionsMenu(menu); 
    menu.clear(); 
    // Add menus here 
    setMenuBackground(); 
    return true; 
} 

這完全可以讓你改變的背景選項菜單轉換爲任何顏色或圖像(在本例中爲白色)。

+0

感謝您的回答,但我正在尋找另一種溶劑,因爲這會覆蓋我的應用程序外部的某些對話框的外觀,例如,我收到短信時的ChompSMS對話框 – Addev

+0

這隻會改變選項菜單的背景和沒有其他的。 –

+2

如果您在項目中使用支持庫,此解決方案將無法工作並拋出IllegalStateException(已在此LayoutInflater上設置了一個工廠)。 –