How can I change the Options menu background for Android 2.3?

前端 未结 6 971
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 11:18

I have the same standard options menu, but I want to change the background of the items from white to black. I\'ve seen many postings about how to do it, but those don\'t wo

6条回答
  •  旧时难觅i
    2020-12-15 11:38

    The below code is working fine 2.3.6(test on device and emulator). It is almost copied & combined from different sites from google search

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        setMenuBackground();
        return super.onCreateOptionsMenu(menu);
    }
    
    protected void setMenuBackground() {
        getLayoutInflater().setFactory(new Factory() {
         @Override
                    public View onCreateView(final String name, final Context context,
                            final AttributeSet attrs) {
    
                        if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
    
                            try { // Ask our inflater to create the view
                                final LayoutInflater f = getLayoutInflater();
                                final View[] view = new View[1];
                                try {
                                    view[0] = f.createView(name, null, attrs);
                                } catch (InflateException e) {
                                    hackAndroid23(name, attrs, f, view);
                                }
                                // Kind of apply our own background
                                new Handler().post(new Runnable() {
                                    public void run() {
                                        view[0].setBackgroundColor(Color.BLUE);
    
                                    }
                                });
                                return view[0];
                            } catch (InflateException e) {
                            } catch (ClassNotFoundException e) {
    
                            }
                        }
                        return null;
                    }
                });
            }
    
            static void hackAndroid23(final String name,
                    final android.util.AttributeSet attrs, final LayoutInflater f,
                    final View[] view) {
                try {
                    f.inflate(new XmlPullParser() {
                        @Override
                        public int next() throws XmlPullParserException, IOException {
                            try {
                                view[0] = (TextView) f.createView(name, null, attrs);
                            } catch (InflateException e) {
                            } catch (ClassNotFoundException e) {
                            }
                            throw new XmlPullParserException("exit");
                        }
    
                        @Override
                        public void defineEntityReplacementText(String entityName,
                                String replacementText)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public int getAttributeCount() {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public String getAttributeName(int index) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getAttributeNamespace(int index) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getAttributePrefix(int index) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getAttributeType(int index) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getAttributeValue(int index) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getAttributeValue(String namespace,
                                String name) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public int getColumnNumber() {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public int getDepth() {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public int getEventType() throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public boolean getFeature(String name) {
                            // TODO Auto-generated method stub
                            return false;
                        }
    
                        @Override
                        public String getInputEncoding() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public int getLineNumber() {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public String getName() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getNamespace() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getNamespace(String prefix) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public int getNamespaceCount(int depth)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public String getNamespacePrefix(int pos)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getNamespaceUri(int pos)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getPositionDescription() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getPrefix() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public Object getProperty(String name) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public String getText() {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public char[] getTextCharacters(
                                int[] holderForStartAndLength) {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public boolean isAttributeDefault(int index) {
                            // TODO Auto-generated method stub
                            return false;
                        }
    
                        @Override
                        public boolean isEmptyElementTag()
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return false;
                        }
    
                        @Override
                        public boolean isWhitespace() throws XmlPullParserException {
                            // TODO Auto-generated method stub
                            return false;
                        }
    
                        @Override
                        public int nextTag() throws XmlPullParserException,
                                IOException {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public String nextText() throws XmlPullParserException,
                                IOException {
                            // TODO Auto-generated method stub
                            return null;
                        }
    
                        @Override
                        public int nextToken() throws XmlPullParserException,
                                IOException {
                            // TODO Auto-generated method stub
                            return 0;
                        }
    
                        @Override
                        public void require(int type, String namespace, String name)
                                throws XmlPullParserException, IOException {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void setFeature(String name, boolean state)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void setInput(Reader in)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void setInput(InputStream inputStream,
                                String inputEncoding) throws XmlPullParserException {
                            // TODO Auto-generated method stub
    
                        }
    
                        @Override
                        public void setProperty(String name, Object value)
                                throws XmlPullParserException {
                            // TODO Auto-generated method stub
    
                        }
                    }, null, false);
                } catch (InflateException e1) {
                    // "exit" ignored
                }
            }
    

提交回复
热议问题