由Spannable String构建器设置选项菜单项文本时,onOptionsItemSelected不起作用

时间:2014-02-18 05:48:52

标签: android textcolor optionmenu

最近我正在努力改变菜单项背景,我通过我在那里找到的代码进行更改 这是代码

 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:android.util.EventLong.writeEvent中的有效负载项类型无效    (nativemethod)在android app.activity.onMenuItemSelected(Activity.java:2533)....

1 个答案:

答案 0 :(得分:0)

你是否正在这样做以改变溢出菜单按钮中元素文本的颜色?

如果这是尝试,you should instead simply change the theme of your app by using the Android Asset Studio。这将为您的应用生成完整的正确主题,包括菜单溢出项目的背景。

这是official documentation about Themes。基本上,您将在项目的zip中压缩生成的主题的内容,并在AndroidManifest文件中将主题定义为您的应用主题。