android.content.res.TypedArray

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inwhites/article/details/53410426
TypedArray
public class TypedArray 
extends Object 

java.lang.Object
   ↳	android.content.res.TypedArray

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.

我们在定义自己的View时,往往都需要用到一个类TypedArray,这个类究竟是干嘛的呢??


--简单来说TypedArray就是一个属性值的数组容器,你可以从这个容器里面的到你想要的属性值。

我们自己定义的View有一些属性是View本身没有的,需要我们自己去定义,定义就定义吧,也不是什么难事,直接在MyView里面定义几个变量不就行了吗?然后再通过该类的初始化后,给这些属性值赋值就行了,这就是我一开始的想法。。。。直到有一天从GitHub clone了一个非常酷炫的View,也想自己做一个,然后自己就重写了这个类,发现这个View的属性有超过10多个。。。按照自己的想法,这个时候再一个个去赋值,这段代码我自己都看不下去了!!,不能忍啊!所以就回头看看大神们怎么解决的,于是就发现了TypedArray和attrs.xml。


自定义的View有四个构造器

public View(Context context) 
public View(Context context, AttributeSet attrs) 
public View(Context context, AttributeSet attrs, int defStyleAttr) 
public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) 
    
 

我们所需要的属性就是从 AttributeSetattrs这类中的到的。

我们怎么得到一个TypedArray类呢,一般都是obtainStyledAttributes这个方法,见第一幅图。例如

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myview);就可以得到myview下的属性了,这里需要简单说一下自定义的属性是怎么实现的。

一般都是在我们的res/values/ 下建一个名为attrs.xml文件,我们知道xml一个作用就是存储数据,html用于表现数据。这样我们就可以将自定义的属性添加到该文件中,下面是一个实例

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="myview">
        <attr name="line_color" format="color" />
        <attr name="line_width" format="integer" />
    </declare-styleable>

</resources>


上面定义myview的两个属性,分别是line_color和line_width,属性值格式是color和integer,在attrs.xml里面可以定义好多的styleable,只要需要。

这里我们只是定义了属性名,并没有实际的值,我们在重写onDraw()方法的时候还是要用到的,那么怎么得到这些属性的值呢?我们在我们创建的布局文件里给这些属性赋值,就像我们View 的android:layout_width="200dp",这样子就可以给这些属性赋值了。然后就是通过TypedArray的方法得到这些值了例如

int lineWidth = ta.getInteger(R.styleable.myview_line_width, DEFAULT_LINE_WIDTH);这个方法就能得到我们的属性值了,考虑到不是所有属性开发者都会去赋值,一般我们会定义一些默认值,如果开发者没有在布局文件中赋值的话,那么该该方法的返回值就是默认值了。然后就可以使用这些值了。最后需要强调的是TypedArray用完后需要recycle,至于原因,没有了解。

总的来说,这种View 属性值的定义方式都是通过解析xml文件得到属性值的(系统自动完成),确实使得开发变得更容易处理数据,也便于维护.下面附上TypedArray的方法


Public methods

boolean getBoolean(int index, boolean defValue)

Retrieve the boolean value for the attribute at index.

int getChangingConfigurations()

Return a mask of the configuration parameters for which the values in this typed array may change.

int getColor(int index, int defValue)

Retrieve the color value for the attribute at index.

ColorStateList getColorStateList(int index)

Retrieve the ColorStateList for the attribute at index.

float getDimension(int index, float defValue)

Retrieve a dimensional unit attribute at index.

int getDimensionPixelOffset(int index, int defValue)

Retrieve a dimensional unit attribute at index for use as an offset in raw pixels.

int getDimensionPixelSize(int index, int defValue)

Retrieve a dimensional unit attribute at index for use as a size in raw pixels.

Drawable getDrawable(int index)

Retrieve the Drawable for the attribute at index.

float getFloat(int index, float defValue)

Retrieve the float value for the attribute at index.

float getFraction(int index, int base, int pbase, float defValue)

Retrieves a fractional unit attribute at index.

int getIndex(int at)

Returns an index in the array that has data.

int getIndexCount()

Return the number of indices in the array that actually have data.

int getInt(int index, int defValue)

Retrieve the integer value for the attribute at index.

int getInteger(int index, int defValue)

Retrieve the integer value for the attribute at index.

int getLayoutDimension(int index, int defValue)

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.

int getLayoutDimension(int index, String name)

Special version of getDimensionPixelSize(int, int) for retrieving ViewGroup's layout_width and layout_height attributes.

String getNonResourceString(int index)

Retrieves the string value for the attribute at index, but only if that string comes from an immediate value in an XML file.

String getPositionDescription()

Returns a message about the parser state suitable for printing error messages.

int getResourceId(int index, int defValue)

Retrieves the resource identifier for the attribute at index.

Resources getResources()

Returns the Resources object this array was loaded from.

String getString(int index)

Retrieves the string value for the attribute at index.

CharSequence getText(int index)

Retrieves the styled string value for the attribute at index.

CharSequence[] getTextArray(int index)

Retrieve the CharSequence[] for the attribute at index.

int getType(int index)

Returns the type of attribute at the specified index.

boolean getValue(int index, TypedValue outValue)

Retrieve the raw TypedValue for the attribute at index.

boolean hasValue(int index)

Determines whether there is an attribute at index.

boolean hasValueOrEmpty(int index)

Determines whether there is an attribute at index, returning true if the attribute was explicitly set to @empty and false only if the attribute was undefined.

int length()

Returns the number of values in this array.

TypedValue peekValue(int index)

Retrieve the raw TypedValue for the attribute at index and return a temporary object holding its data.

void recycle()

Recycles the TypedArray, to be re-used by a later caller.

String toString()

Returns a string representation of the object.




猜你喜欢

转载自blog.csdn.net/inwhites/article/details/53410426