无鸯 Android中自定义属性(attrs.xml,TypedArray)的使用,

这个例子比较全,很不错,

转自 http://www.oschina.net/code/snippet_163910_6283


package com.adnroid.test; 

02   
03import com.adnroid.test.R; 
04   
05import android.content.Context; 
06import android.content.res.TypedArray; 
07import android.graphics.Canvas; 
08import android.graphics.Color; 
09import android.graphics.Paint; 
10import android.graphics.Rect; 
11import android.graphics.Paint.Style; 
12import android.util.AttributeSet; 
13import android.view.View; 
14   
15public class MyView extends View { 
16    private Paint myPaint; 
17    private static final String myString = "Welcome to our Zoon!"
18   
19    public MyView(Context context) { 
20        super(context); 
21        // TODO Auto-generated constructor stub 
22     
23       
24    public MyView(Context context, AttributeSet attr) { 
25        super(context, attr); 
26         myPaint = new Paint(); 
27         TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);//TypedArray是一个数组容器 
28        float textSize = a.getDimension(R.styleable.myView_textSize, 30);//防止在XML文件里没有定义,就加上了默认值30 
29        int textColor = a.getColor(R.styleable.myView_textColor, 0xFFFFFFFF);//同上,这里的属性是:名字_属性名 
30         myPaint.setTextSize(textSize); 
31         myPaint.setColor(textColor); 
32         a.recycle();//我的理解是:返回以前取回的属性,供以后使用。以前取回的可能就是textSize和textColor初始化的那段 
33     
34    @Override 
35    protected void onDraw(Canvas canvas) { 
36        // TODO Auto-generated method stub 
37        super.onDraw(canvas); 
38        //myPaint = new Paint(); 
39         myPaint.setColor(Color.RED); 
40         myPaint.setStyle(Style.FILL); 
41           
42         canvas.drawRect(new Rect(10,10,100,100), myPaint); 
43         myPaint.setColor(Color.WHITE); 
44         canvas.drawText(myString, 10, 100, myPaint); 
45     
46   
47

2. [代码]attrs.xml    

1<?xml version="1.0" encoding="utf-8"?> 
2<resources
3    <declare-styleable name="myView"
4        <attr name="textColor" format="color"/> 
5        <attr name="textSize" format="dimension"/> 
6    </declare-styleable
7</resources

3. [代码]main.xml    

01<?xml version="1.0" encoding="utf-8"?> 
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03    xmlns:test="http://schemas.android.com/apk/res/com.adnroid.test" 
04    android:orientation="vertical" 
05    android:layout_width="fill_parent" 
06    android:layout_height="fill_parent" 
07    
08<TextView   
09    android:layout_width="fill_parent"  
10    android:layout_height="wrap_content"  
11    android:text="@string/hello" 
12    /> 
13<com.adnroid.test.MyView 
14    android:layout_width="fill_parent" 
15    android:layout_height="fill_parent" 
16    test:textSize="10px" 
17    test:textColor="#fff" 
18    /> 
19</LinearLayout

4. [图片] 6c32391db1a8d7cd86d6b6bc.jpg.png    


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值