2012-04-27 63 views
1

我有我的菜单选项,背景色改为黑色为Android 2.2以上,我给出的解决方案试了一下:如何更改菜单背景色为黑色在Android 2.2及更高

protected void setMenuBackground(){      
     // Log.d(TAG, "Enterting setMenuBackGround"); 
     getLayoutInflater().setFactory(new Factory() { 
      public View onCreateView(String name, Context context, AttributeSet attrs) { 
       if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { 
        try { // Ask our inflater to create the view 
         LayoutInflater f = getLayoutInflater(); 
         final View view = f.createView(name, null, attrs); 

         new Handler().post(new Runnable() { 
          public void run() { 
           // sets the background color 
           view.setBackgroundResource(R.color.black); 
           // sets the text color    
           ((TextView) view).setTextColor(Color.WHITE); 
           // sets the text size    
           ((TextView) view).setTextSize(18); 
       } 
         }); 
        return view; 
       } 
      catch (InflateException e) {} 
      catch (ClassNotFoundException e) {} 
     } 
     return null; 
      } 
    }); 
    } 

但显示致命异常错误 “04-27 17:03:38.831:E/AndroidRuntime(923):java.lang.IllegalStateException:已在此LayoutInflater上设置了一个工厂”。难道我做错了什么 ??

回答