Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

一、 在res/values 文件下定义一个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>  

二、 我们在MyView.java 代码编写如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ) 防止我们在xml 文件中没有定义.从而使用默认值!

MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

  1. public MyView(Context context,AttributeSet attrs)  
  2.     {  
  3.         super(context,attrs);  
  4.         mPaint = new Paint();  
  5.           
  6.         TypedArray a = context.obtainStyledAttributes(attrs,  
  7.                 R.styleable.MyView);  
  8.           
  9.         int textColor = a.getColor(R.styleable.MyView_textColor,  
  10.                 0XFFFFFFFF);  
  11.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  12.           
  13.         mPaint.setTextSize(textSize);  
  14.         mPaint.setColor(textColor);  
  15.           
  16.         a.recycle();  
  17.     }  

MyView.java  MyView控件全部代码如下 

  1. public class MyView extends View {  
  2.     private Paint mPaint;  
  3.     private Context mContext;  
  4.     private static final String mString = "Welcome to Mr Wei's blog";  
  5.       
  6.     public MyView(Context context) {  
  7.         super(context);  
  8.         mPaint = new Paint();  
  9.     }  
  10.     public MyView(Context context,AttributeSet attrs)  
  11.     {  
  12.         super(context,attrs);  
  13.         mPaint = new Paint();  
  14.           
  15.         TypedArray a = context.obtainStyledAttributes(attrs,  
  16.                 R.styleable.MyView);  
  17.           
  18.         int textColor = a.getColor(R.styleable.MyView_textColor,  
  19.                 0XFFFFFFFF);  
  20.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  21.           
  22.         mPaint.setTextSize(textSize);  
  23.         mPaint.setColor(textColor);  
  24.           
  25.         a.recycle();  
  26.     }  
  27.     @Override  
  28.     protected void onDraw(Canvas canvas) {  
  29.         // TODO Auto-generated method stub  
  30.         super.onDraw(canvas);  
  31.         //设置填充  
  32.         mPaint.setStyle(Style.FILL);  
  33.           
  34.         //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
  35.         canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
  36.           
  37.         mPaint.setColor(Color.BLUE);  
  38.         //绘制文字  
  39.         canvas.drawText(mString, 10, 110, mPaint);  
  40.     }  
  41. }  

三、将我们自定义的MyView 加入布局main.xml 文件中,并且使用自定义属性,自定义属性必须加上:

    " xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor"  ,test是自定义属性的前缀,com.android.tutor 是我们包名.

main.xml 全部代码如下:

  1. <?xml   
  2. version="1.0" encoding="utf-8"?>  
  3. <LinearLayout   
  4. xmlns:android="http://schemas.android.com/apk/res/android"  
  5.                 
  6. xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"  
  7.     android:orientation="vertical"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent"  
  10.     >  
  11. <TextView    
  12.     android:layout_width="fill_parent"   
  13.     android:layout_height="wrap_content"   
  14.     android:text="@string/hello"  
  15.     />  
  16. <com.android.tutor.MyView  
  17.     android:layout_width="fill_parent"   
  18.     android:layout_height="fill_parent"   
  19.     test:textSize="20px"  
  20.     test:textColor="#fff"  
  21. />  
  22. </LinearLayout>  

format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:

1. reference:参考某一资源ID,以此类推

(1)属性定义:

(2)属性使用:

<imageview< p="">

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID"

/>

2. color:颜色值

3. boolean:布尔值

4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换

5. float:浮点值。

6. integer:整型值。

7. string:字符串

8. fraction:百分数。

9. enum:枚举值

10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。

11. reference|color:颜色的资源文件。 12.reference|boolean:布尔值的资源文件

注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须personattr:name="@string/app_name"这种格式,否则会出错

原文地址:https://www.cnblogs.com/lianghui66/p/4074143.html