1

最近我正在更改菜单项背景,我通过代码更改它,我发现这里是代码

 public void setMenuBackground() {
    // TODO Auto-generated method stub

    inflater.setFactory(new Factory() {

        public View onCreateView(String name, Context context,
                AttributeSet attrs) {
            // TODO Auto-generated method stub
            if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                LayoutInflater f = (LayoutInflater) cxt
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View[] view = new View[1];
                try {
                    view[0] = f.createView(name, null, attrs);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InflateException e) {
                    // TODO Auto-generated catch block
                    hackAndroid23(name, attrs, f, view);
                }
                new Handler().post(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        view[0].setBackgroundColor(Color.WHITE);
                    }

                });
                return view[0];
            }
            return null;
        }

    });
}

static void hackAndroid23(final String name,
        final android.util.AttributeSet attrs, final LayoutInflater f,
        final View[] view) {
    try {
        f.inflate(new XmlPullParser() {

            public int next() throws XmlPullParserException, IOException {
                try {
                    view[0] = f.createView(name, null, attrs);
                } catch (InflateException e) {
                } catch (ClassNotFoundException e) {
                }
                throw new XmlPullParserException("exit");
            }

            public void defineEntityReplacementText(String entityName,
                    String replacementText) throws XmlPullParserException {
                // TODO Auto-generated method stub

            }

            public int getAttributeCount() {
                // TODO Auto-generated method stub
                return 0;
            }

            public String getAttributeName(int index) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getAttributeNamespace(int index) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getAttributePrefix(int index) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getAttributeType(int index) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getAttributeValue(int index) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getAttributeValue(String namespace, String name) {
                // TODO Auto-generated method stub
                return null;
            }

            public int getColumnNumber() {
                // TODO Auto-generated method stub
                return 0;
            }

            public int getDepth() {
                // TODO Auto-generated method stub
                return 0;
            }

            public int getEventType() throws XmlPullParserException {
                // TODO Auto-generated method stub
                return 0;
            }

            public boolean getFeature(String name) {
                // TODO Auto-generated method stub
                return false;
            }

            public String getInputEncoding() {
                // TODO Auto-generated method stub
                return null;
            }

            public int getLineNumber() {
                // TODO Auto-generated method stub
                return 0;
            }

            public String getName() {
                // TODO Auto-generated method stub
                return null;
            }

            public String getNamespace() {
                // TODO Auto-generated method stub
                return null;
            }

            public String getNamespace(String prefix) {
                // TODO Auto-generated method stub
                return null;
            }

            public int getNamespaceCount(int depth)
                    throws XmlPullParserException {
                // TODO Auto-generated method stub
                return 0;
            }

            public String getNamespacePrefix(int pos)
                    throws XmlPullParserException {
                // TODO Auto-generated method stub
                return null;
            }

            public String getNamespaceUri(int pos)
                    throws XmlPullParserException {
                // TODO Auto-generated method stub
                return null;
            }

            public String getPositionDescription() {
                // TODO Auto-generated method stub
                return null;
            }

            public String getPrefix() {
                // TODO Auto-generated method stub
                return null;
            }

            public Object getProperty(String name) {
                // TODO Auto-generated method stub
                return null;
            }

            public String getText() {
                // TODO Auto-generated method stub
                return null;
            }

            public char[] getTextCharacters(int[] holderForStartAndLength) {
                // TODO Auto-generated method stub
                return null;
            }

            public boolean isAttributeDefault(int index) {
                // TODO Auto-generated method stub
                return false;
            }

            public boolean isEmptyElementTag()
                    throws XmlPullParserException {
                // TODO Auto-generated method stub
                return false;
            }

            public boolean isWhitespace() throws XmlPullParserException {
                // TODO Auto-generated method stub
                return false;
            }

            public int nextTag() throws XmlPullParserException, IOException {
                // TODO Auto-generated method stub
                return 0;
            }

            public String nextText() throws XmlPullParserException,
                    IOException {
                // TODO Auto-generated method stub
                return null;
            }

            public int nextToken() throws XmlPullParserException,
                    IOException {
                // TODO Auto-generated method stub
                return 0;
            }

            public void require(int type, String namespace, String name)
                    throws XmlPullParserException, IOException {
                // TODO Auto-generated method stub

            }

            public void setFeature(String name, boolean state)
                    throws XmlPullParserException {
                // TODO Auto-generated method stub

            }

            public void setInput(Reader in) throws XmlPullParserException {
                // TODO Auto-generated method stub

            }

            public void setInput(InputStream inputStream,
                    String inputEncoding) throws XmlPullParserException {
                // TODO Auto-generated method stub

            }

            public void setProperty(String name, Object value)
                    throws XmlPullParserException {
                // TODO Auto-generated method stub

            }
        }, null, false);
    } catch (InflateException e1) {
        // "exit" ignored
    }
}

它对我来说很好,但是当我通过代码更改菜单的文本颜色时

  SpannableStringBuilder text = new SpannableStringBuilder();
        text.append(str4);
        text.setSpan(new ForegroundColorSpan(Color.BLACK), 0,
                text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 item.setTitle(text);

我得到了黑色的文本,但问题是当按下文本颜色改变的按钮时,它会给我以下错误

java.lang.IllegalArgumentException: Invalid payload item type at android.util.EventLong.writeEvent (nativemethod) at android app.activity.onMenuItemSelected(Activity.java:2533)....

4

1 回答 1

0

你这样做是为了改变溢出菜单按钮中元素文本的颜色吗?

如果这是尝试,您应该改为使用 Android Asset Studio 更改应用程序的主题。这将为您的应用程序生成完整的正确主题,包括菜单溢出项的背景。

这是有关 Themes 的官方文档。基本上,您将在项目的 zip 中压缩生成的主题的内容,并将主题定义为 AndroidManifest 文件中的应用程序主题。

于 2014-02-18T05:56:30.893 回答