android 解析xml 到 AttributeSet对象

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:attr="http://schemas.android.com/apk/res/com.example.powerimageviewtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.example.powerimageviewtest.PowerImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/anim"
        attr:auto_play="false"
        android:background="@drawable/ic_launcher"
        />


</RelativeLayout>

public PowerImageView(Context context, AttributeSetattrs) {

attributeSet 是在layoutinflat 中属值的



通过索引访问

attrs.getAttributeCount()  返回 7  属性的个数

 attrs.getAttributeName(i) 第i个属性的name


for (int i = 0; i < attrs.getAttributeCount(); i++) {
			// 11-11 14:50:36.835: I/System.out(7639): fd-->id
			// 11-11 14:50:36.835: I/System.out(7639): fd-->background
			// 11-11 14:50:36.835: I/System.out(7639): fd-->layout_width
			// 11-11 14:50:36.835: I/System.out(7639): fd-->layout_height
			// 11-11 14:50:36.835: I/System.out(7639): fd-->src
			// 11-11 14:50:36.835: I/System.out(7639):
			// fd-->layout_centerInParent
			// 11-11 14:50:36.835: I/System.out(7639): fd-->auto_play
			// 不明白顺序是怎么来的
			syso("fd-->" + attrs.getAttributeName(i));
		}
从下面可看出,其实是xmlPullParser


attrs.getAttributeResourceValue(1, 0)     第1个元素的资源id 本例
ic_launcher 对应的id

attrs.getAttributeBooleanValue(6, false)  第6个元素的布尔值


通过命名空间访问:

String androidNM = "http://schemas.android.com/apk/res/android";
		String myNM = "http://schemas.android.com/apk/res/com.example.powerimageviewtest";
		syso("fd-->" + attrs.getAttributeBooleanValue(myNM, "auto_play", false)); // false
		syso("fd-->" + attrs.getAttributeResourceValue(androidNM, "src", 0)); // 2130837504



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值