/**
* Returns a ChipDrawable from the given XML resource. All attributes from {@link
* R.styleable#ChipDrawable} and a custom <code>style</code> attribute are supported. A chip
* resource may look like:
*
* <pre>{@code
* <chip
* xmlns:app="http://schemas.android.com/apk/res-auto"
* style="@style/Widget.MaterialComponents.Chip.Entry"
* app:chipIcon="@drawable/custom_icon"/>
* }</pre>
*/
public static ChipDrawable createFromResource(Context context, @XmlRes int id) {
try {
XmlPullParser parser = context.getResources().getXml(id);
int type;
do {
type = parser.next();
} while (type != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT);
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
if (!TextUtils.equals(parser.getName(), "chip")) {
throw new XmlPullParserException("Must have a <chip> start tag");
}
AttributeSet attrs = Xml.asAttributeSet(parser);
@StyleRes int style = attrs.getStyleAttribute();
if (style == 0) {
style = R.style.Widget_MaterialComponents_Chip_Entry;
}
return createFromAttributes(context, attrs, R.attr.chipStandaloneStyle, style);
} catch (XmlPullParserException | IOException e) {
Resources.NotFoundException exception =
new NotFoundException("Can't load chip resource ID #0x" + Integer.toHexString(id));
exception.initCause(e);
throw exception;
}
}
ChipDrawable.java 文件源码
java
阅读 37
收藏 0
点赞 0
评论 0
项目:material-components-android
作者:
评论列表
文章目录