1.在attr中定义自己的样式
<resources>
<declare-styleable name="Favorite">
<attr name="className" format="string" />
<attr name="packageName" format="string" />
<attr name="screen" format="string" />
<attr name="x" format="string" />
<attr name="y" format="string" />
</declare-styleable>
</resources>
2.定义自己的xml属性
<favorites xmlns:
test="http://schemas.android.com/apk/res/
com.android.google">为包名
<favorite test:className="com.test.contacts.DialtactsContactsEntryActivity"
test:packageName="com.test.contacts" test:screen="1" test:x="1"
test:y="3"></favorite>
<favorite test:className="com.google.test" test:packageName="com.test.hello"
test:screen="2" test:x="2" test:y="2"></favorite>
</favorites>
2。TypedArray a = this.obtainStyledAttributes(set,R.styleable.Favorite);//把xml中定义的属性 -->对于到set集合中的值集合
String className=a.getString(R.styleable.Favorite_className);//得到对于的值
3.程序解析的例子
private void parserXml() throws XmlPullParserException, Throwable {
XmlResourceParser parser = getResources()
.getXml(R.xml.default_favorite);
AttributeSet set = Xml.asAttributeSet(parser);
depth = parser.getDepth();
System.out.println(depth);
int type;
while (((type = parser.next()) != XmlPullParser.END_TAG || parser
.getDepth() > depth)
&& type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
TypedArray a = this.obtainStyledAttributes(set,
R.styleable.Favorite);
String name = parser.getName();
if (name.equals("favorite")) {
String packageName = a
.getString(R.styleable.Favorite_packageName);
String className = a.getString(R.styleable.Favorite_className);
String screen = a.getString(R.styleable.Favorite_screen);
String x = a.getString(R.styleable.Favorite_x);
String y = a.getString(R.styleable.Favorite_y);
System.out.println(packageName + "/t" + className + "/t"
+ screen + "/t" + x + "/t" + y);
}
a.recycle();//
}
}