SimpleInflaterpublic class SimpleInflater extends Object
Fields Summary |
---|
private static final String | XML_MENUMenu tag name in XML. | private static final String | XML_GROUPGroup tag name in XML. | private static final String | XML_ITEMItem tag name in XML. | private android.content.Context | mContext |
Methods Summary |
---|
public void | inflate(int menuRes)
XmlResourceParser parser = null;
try {
parser = mContext.getResources().getLayout(menuRes);
AttributeSet attrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs);
} catch (XmlPullParserException e) {
throw new InflateException("Error inflating menu XML", e);
} catch (IOException e) {
throw new InflateException("Error inflating menu XML", e);
} finally {
if (parser != null) parser.close();
}
| private void | parseMenu(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)
int eventType = parser.getEventType();
String tagName;
boolean lookingForEndOfUnknownTag = false;
String unknownTagName = null;
// This loop will skip to the menu start tag
do {
if (eventType == XmlPullParser.START_TAG) {
tagName = parser.getName();
if (tagName.equals(XML_MENU)) {
// Go to next tag
eventType = parser.next();
break;
}
throw new RuntimeException("Expecting menu, got " + tagName);
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
boolean reachedEndOfMenu = false;
while (!reachedEndOfMenu) {
switch (eventType) {
case XmlPullParser.START_TAG:
if (lookingForEndOfUnknownTag) {
break;
}
tagName = parser.getName();
if (tagName.equals(XML_ITEM)) {
readItem(attrs);
} else if (tagName.equals(XML_MENU)) {
parseMenu(parser, attrs);
} else {
lookingForEndOfUnknownTag = true;
unknownTagName = tagName;
}
break;
case XmlPullParser.END_TAG:
tagName = parser.getName();
if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
lookingForEndOfUnknownTag = false;
unknownTagName = null;
} else if (tagName.equals(XML_ITEM)) {
} else if (tagName.equals(XML_MENU)) {
reachedEndOfMenu = true;
}
break;
case XmlPullParser.END_DOCUMENT:
throw new RuntimeException("Unexpected end of document");
}
eventType = parser.next();
}
| public void | readItem(android.util.AttributeSet attrs)
TypedArray a = mContext.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.MenuItem);
// Inherit attributes from the group as default value
int itemId = a.getResourceId(R.styleable.MenuItem_android_id, 0);
final int category = a.getInt(R.styleable.MenuItem_android_menuCategory, 0);
final int order = a.getInt(R.styleable.MenuItem_android_orderInCategory, 0);
CharSequence itemTitle = a.getText(R.styleable.MenuItem_android_title);
CharSequence itemTitleCondensed = a.getText(R.styleable.MenuItem_android_titleCondensed);
int itemIconResId = a.getResourceId(R.styleable.MenuItem_android_icon, 0);
String itemAlphabeticShortcut = a.getString(R.styleable.MenuItem_android_alphabeticShortcut);
String itemNumericShortcut = a.getString(R.styleable.MenuItem_android_numericShortcut);
int itemCheckable = 0;
if (a.hasValue(R.styleable.MenuItem_android_checkable)) {
// Item has attribute checkable, use it
itemCheckable = a.getBoolean(R.styleable.MenuItem_android_checkable, false) ? 1 : 0;
}
boolean itemChecked = a.getBoolean(R.styleable.MenuItem_android_checked, false);
boolean itemVisible = a.getBoolean(R.styleable.MenuItem_android_visible, false);
boolean itemEnabled = a.getBoolean(R.styleable.MenuItem_android_enabled, false);
a.recycle();
|
|