---
title: "Android中自定义属性（attrs.xml，TypedArray）的使用【转】"
author: "cooper100"
date: "2012-05-25T11:09:28Z"
tags: []
categories: ["移动开发"]
slug: "android-attrs-xml-typedarray"
draft: false
lastmod: "2012-05-25T11:09:08Z"
id: 1540371
url: "https://cooper100.iteye.com/blog/1540371"
---

该实例是在自定义View上使用自定义属性的。 先来看看源码：MyView.java Code: package com.adnroid.test; import com.adnroid.test.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View { private Paint myPaint; private static final String myString = "Welcome to our Zoon!"; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context, AttributeSet attr) { super(context, attr); myPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);//TypedArray是一个数组容器 float textSize = a.getDimension(R.styleable.myView\_textSize, 30);//防止在XML文件里没有定义，就加上了默认值30 int textColor = a.getColor(R.styleable.myView\_textColor, 0xFFFFFFFF);//同上,这里的属性是:名字\_属性名 myPaint.setTextSize(textSize); myPaint.setColor(textColor); a.recycle();//我的理解是：返回以前取回的属性，供以后使用。以前取回的可能就是textSize和textColor初始化的那段 } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //myPaint = new Paint(); myPaint.setColor(Color.RED); myPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10,10,100,100), myPaint); myPaint.setColor(Color.WHITE); canvas.drawText(myString, 10, 100, myPaint); } } attrs.xml Code: main.xml Code: 最终的效果