AttributeSet与TypeArray的关系

相信大家都对这连个比较好奇,我们在实现自定义View的时候,常常使用TypeArray来加载xml属性值,那么在自定义View的构造方法中AttributeSet也出现了,那么他的出现有什么用呢,我们来具体看一下

先在Value里面新建attrs.xml文件
文件定义如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="textSize" format="integer"></attr>
        <attr name="textColor" format="reference|color"></attr>
    </declare-styleable>
</resources>

下面是Activity_mainde布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:user="http://schemas.android.com/apk/res/com.example.fang.myapplication"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fang.myapplication.MainActivity">

    <com.example.fang.myapplication.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        user:textColor="@color/colorAccent"
        user:textSize="20" />

</LinearLayout>

在上面是自定义View,在linearlayout中我们定义了一个命名空间,地址是http://schemas.android.com/apk/res/自己的完整包名;

接着我们新建MyView继承View具体代码如下

package com.example.fang.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;



/**
 * Created by fang on 2017/11/10.
 */

public class MyView extends View {

   String TAG="main";
    public MyView(Context context) {
        this(context,null);

    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
          int count= attrs.getAttributeCount();
        for(int i=0;i<count;i++){
          String name=  attrs.getAttributeName(i);
            String value=attrs.getAttributeValue(i);
            Log.d(TAG, "name="+name+""+"vslue="+value);
        }

    }

}

打印结果是什么呢:我们来看一下这里写图片描述
我们发现答打印了俩行,对应与我们的个属性,即layout_width,
layout_height,textSize,textColor,我们发现通过A他tributeSet访问我们定义View的属性值,只不过奇怪的是我们发现textColor的值居然是乱七八糟的东西,也不知道是什么鬼,那么,这是什么原因呢,我也不清楚,看下官方文档:
A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to Resources.Theme.obtainStyledAttributes() which will take care of parsing the attributes for you. In particular, the Resources API will convert resource references (attribute values such as “@string/my_label” in the original XML) to the desired type for you;
大致意思是,该类是个属性的集合,与xml文件相关联的文档,我们通常不直接使用这个类也是使用Resources.Theme.obtainStyledAttributes()来加载属性,特别是,资源API将把资源引用(原始XML中的“@ string/ my_label”属性值)转换为所需类型。
那么我们在在来试试使用TypeArray来加载,我们将构造方法中的代码进行修改

   public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray type = getResources().obtainAttributes(attrs, R.styleable.MyView);
        int count = type.getIndexCount();
        for (int i = 0; i < count; i++) {
            int index=type.getIndex(i);
            if(index==R.styleable.MyView_textSize){
              int size= type.getInt(index,-1);
                Log.d(TAG,"size="+size);
            }
           if(index==R.styleable.MyView_textColor){
               String color=type.getString(index);
               Log.d(TAG,"color="+color);
           }
        }
    }

打印结果如下
这里写图片描述

我们发现刚刚用AttaibuteSet解析资源引用时出现一大堆数字的情况消失了,很兴奋,现在我们可以推测,TypeArray就是对Attribute的再包装

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章