Android自定义属性时TypedArray的使用方法

来源:互联网 发布:外贸域名网址 编辑:程序博客网 时间:2024/05/17 06:29

转载地址:http://blog.csdn.net/eyu8874521/article/details/8552534

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

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

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="MyView">  
  4.         <attr name="myTextSize" format="dimension"/>  
  5.         <attr name="myColor" format="color"/>  
  6.     </declare-styleable>  
  7. </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的布局文件中键入如下的一行:

xmlns:myapp=http://schemas.android.com/apk/res/com.eyu.attrtextdemo绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:myapp="http://schemas.android.com/apk/res/com.eyu.attrtextdemo"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="@string/hello_world" />  
  13.     <com.eyu.attrtextdemo.MyView  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_width="wrap_content"  
  16.         myapp:myTextSize="20sp"  
  17.         myapp:myColor="#324243"/>  
  18.   
  19. </LinearLayout>  


 

3》在自定义view的代码中引入自定义属性,修改构造函数

context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置

obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);

调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响  

具体如下:

[java] view plain copy
  1. package com.eyu.attrtextdemo;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Paint.Style;  
  8. import android.util.AttributeSet;  
  9. import android.view.View;  
  10.   
  11. public class MyView extends View{  
  12.     public Paint paint;  
  13.   
  14.     public MyView(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.         paint = new Paint();  
  17.           
  18.         TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);      
  19.         int textColor = a.getColor(R.styleable.MyView_myColor, 003344);  
  20.         float textSize = a.getDimension(R.styleable.MyView_myTextSize, 33);  
  21.         paint.setTextSize(textSize);  
  22.         paint.setColor(textColor);  
  23.         a.recycle();  
  24.     }  
  25.   
  26.     public MyView(Context context) {  
  27.         super(context);  
  28.         // TODO Auto-generated constructor stub  
  29.     }  
  30.       
  31.     @Override  
  32.     protected void onDraw(Canvas canvas) {  
  33.         // TODO Auto-generated method stub  
  34.         super.onDraw(canvas);     
  35.         paint.setStyle(Style.FILL);  
  36.         canvas.drawText("aaaaaaa"1050, paint);  
  37.     }  
  38.       
  39.       
  40.       
  41. }  

 

运行后:
                                        


注意事项:

       (a) 增加xmlns:app="http://schemas.android.com/apk/res-auto"

       (b) app:background_color="@color/colorBlack"

 


0 0
原创粉丝点击