最近我正在更改菜单项背景,我通过代码更改它,我发现这里是代码
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)....