How to properly write XML for AttributeSet?

自作多情 提交于 2019-12-06 04:12:46

First of all you must define the xml properties in a single file .xml inside the xml folder (subfolder of the resources folder) similarly as you would inside a layout file.

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentDescription="@string/no_descr"
    android:src="@drawable/dummy"/>

Secondly retrieving the attribute set is not as easy as two lines of code. You'll need the following function written in Scala. Sorry I am a Scala guy but if you are stuck with java then you can easily convert it!

def getAttributeSetFromXml(xmlId: Int, tagName: String, resources: Resources): AttributeSet = {
/**
 * The good thing for being an internal function is that we don't need to pass tagName as a ref
 */
def getAttributeSet(xmlPullParser: XmlPullParser /*, tagName: String*/): AttributeSet = {
  val state = xmlPullParser.next();
  if (state == XmlPullParser.START_TAG &&
    (xmlPullParser.getName contains tagName)) {
    Xml.asAttributeSet(xmlPullParser);
  }
  else {
    if (state == XmlPullParser.END_DOCUMENT) null;
    else getAttributeSet(xmlPullParser /*, tagName*/);
  }
}

getAttributeSet(resources.getXml(xmlId) /*, tagName*/);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!