XmlResourceParser实现

Mug*_*gur 6 android interface-implementation

我需要从服务器动态加载xml布局.LayoutInflater具有使用XmlPullParser的inflate方法.我试过了,但它不起作用.

查看Android源代码,结果发现这些inflate方法是使用XmlResourceParser调用的.Android使用的实现是XmlBlock.Parser,但这不是公共API.

我可以使用XmlResourceParser公共实现吗?

Ant*_*ues 4

您可以使用Android 文档XmlPullParser中描述的传统方式:

InputStream yourRemoteLayout = ...;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(yourRemoteLayout, "someEncoding");
AttributeSet attributes = Xml.asAttributeSet(parser);
Run Code Online (Sandbox Code Playgroud)

请参阅XmlPullParser 文档中的解释以了解更多详细信息。


编辑:来自LayoutInflater#inflate()文档:

Important   For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
Run Code Online (Sandbox Code Playgroud)

LayoutInflater.Factory2我的猜测是,如果 Android 自己的仅依赖于预处理资源,也许您应该自己实现。