资源类型之TypedArray

TypedArray定义在XML文件中。可以使用它来创建一组其它资源,例如drawables。注意到一组资源并不要求是相同类型的,所以可以创建一组混合类型的资源。但是我们必须知道各个数据类型在组中的具体位置以便可以使用TypedArray's的get...()方法获取每个项目。

Note:一组typed array是一个由名字属性提供(而不是XML文件名)提供的简单资源引用。这样,我们可以把typed array资源同其它简单资源在同一个XML文件中结合起来,结合的方式是放置在<resources>标签下。

FILELOCATION:

         res/values/filename.xml

         文件名是二进制的。<array>元素名将作为调用资源的ID。

COMPILED RESOURCE DATATYPE:

         资源指向的数据类型是TypedArray。

RESOURCE REFERENCE:

         In Java: R.array.array_name

         In XML: @[package:]array.array_name

SYNTAX:

<?xml version="1.0" encoding="utf-8"?>
<resources>
      <array
             name="integer_array_name">
             <item>resource</item>
        </array>
</resource>

EXAMPLE:

XML文件保存在res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
         <array name="icons">
                  <item>@drawable/home</item>
                  <item>@drawable/settings</item>
                  <item>@drawable/logout</item>
         </array>
          <array name="colors">
                   <item>#FFFF0000</item>
                   <item>#FF00FF00</item>
                   <item>#FF0000FF</item>
           </array>
</resources>

下面的代码分别取出array并获取第一个item。

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0, 0);

由上可知TypedArray的用处在于对 一组其它资源的合并,然后又进行分别取出。但事实上这个类型的 更为广泛的应用在于自定义XML属性

以下内容来自http://www.2cto.com/kf/201302/189492.html

有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再具体实现自己定义的复杂view。我们知道在给控件赋属性时,通常使用的是android系统自带的属性,比如 android:layout_height="wrap_content",除此之外,我们亦可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="20sp"的方式了,步骤大致如下:

1》在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="MyView">  
        <attr name="myTextSize" format="dimension"/>  
        <attr name="myColor" format="color"/>  
    </declare-styleable>  
</resources>  
 
其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。
format还可以指定其他的类型比如;
reference   表示引用,参考某一资源ID
string   表示字符串
color   表示颜色值
dimension   表示尺寸值
boolean   表示布尔值
integer   表示整型值
float   表示浮点值
fraction   表示百分数
enum   表示枚举值
flag   表示位运算
 
2》在使用到该自定义view的布局文件中键入如下的一行:
绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:
[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context=".MainActivity" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world" />  
    <com.eyu.attrtextdemo.MyView  
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"  
        myapp:myTextSize="20sp"  
        myapp:myColor="#324243"/>  
  
</LinearLayout>  
 
 
3》在自定义view的代码中引入自定义属性,修改构造函数
context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响  
具体如下:
[java] 
package com.eyu.attrtextdemo;  
  
import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Canvas;  
import android.graphics.Paint;  
import android.graphics.Paint.Style;  
import android.util.AttributeSet;  
import android.view.View;  
  
public class MyView extends View{  
    public Paint paint;  
  
    public MyView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        paint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);      
        int textColor = a.getColor(R.styleable.MyView_myColor, 003344);  
        float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);  
        paint.setTextSize(textSize);  
        paint.setColor(textColor);  
        a.recycle();  
    }  
  
    public MyView(Context context) {  
        super(context);  
        // TODO Auto-generated constructor stub  
    }  
      
    @Override   www.2cto.com
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub  
        super.onDraw(canvas);     
        paint.setStyle(Style.FILL);  
        canvas.drawText("aaaaaaa", 10, 50, paint);  
    }  
      
      
      
}  
 
运行后:
                                        

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值