【问题标题】:How to change menu background color to black in android 2.2 and higher如何在 android 2.2 及更高版本中将菜单背景颜色更改为黑色
【发布时间】:2012-08-24 14:27:01
【问题描述】:

对于 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 上设置了工厂”。我是不是做错了什么??

【问题讨论】:

    标签: android android-menu


    【解决方案1】:

    如果我正确理解您的问题。您应该能够在 XML 中执行此操作。 Override context menu colors in Android

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2015-04-06
      • 2014-02-09
      • 2015-05-30
      • 2019-10-07
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多