打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
定义自己的xml
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();//
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Launcher预设Widget 如果想在Launcher初始化时在Workspace中显示自定义或者网上下载的widget来美化我们的产品,我们需要获取Widget的ComponentName信息,
从零写一个Java WEB框架的演进
Android自定义控件
Android xmlns 的作用及其自定义
Android TextView 自动换行,每行排满的问题
xmlns:android作用以及自定义布局属性
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服