实现自定义属性的三种方法

来源:互联网 发布:淘宝动漫海报店推荐 编辑:程序博客网 时间:2024/05/16 05:07
实现自定义属性主要有三种方法


第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值。

(1)在xml文件中设置属性值

[html] view plaincopy
  1. <com.example.activity.IconTextView   
  2.        android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="@string/smile1"  
  5.     iconSrc="@drawable/smile"/>  

(2)在构造函数中拿到这个值

[java] view plaincopy
  1. public IconTextView(Context context, AttributeSet attrs) {  
  2.         super(context, attrs);  
  3.         resourceID = attrs.getAttributeResourceValue(null"iconSrc"0);  
  4.         if(resourceID > 0) {  
  5.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
  6.         }  
  7.     }  


第二种方法,使用自己的命名空间

(1)注意在xml文件中,需要声明一个命名空间,形式为http:// + 这个VIEW的包名

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:mobile="http://com.example.activity"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.example.activity.IconTextView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/smile1"  
  11.         mobile:iconSrc="@drawable/smile"/>  
  12.   
  13. </LinearLayout>  

(2)通过attrs.getAttributeResourceValue,其中第一个参数为命名空间。

[java] view plaincopy
  1. //命名空间  
  2.    private final String namespace = "http://com.example.activity"  
[java] view plaincopy
  1.     public IconTextView(Context context, AttributeSet attrs) {  
  2.         super(context, attrs);  
  3.         resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc"0);  
  4. //      TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
  5. //      resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
  6.         if(resourceID > 0) {  
  7.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
  8.         }  
  9.     }  


第三种方法,通过自定义attrs.xml来实现

(1)自定义一个attrs.xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="IconTextView">  
  4.         <attr name="iconSrc" format="reference"/>  
  5.     </declare-styleable>  
  6. </resources>  

(2)在xml文件中使用这一属性,注意此时命名空间的书写规范。

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <com.example.activity.IconTextView   
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/smile1"  
  12.         mobile:iconSrc="@drawable/smile"/>  
  13.       
  14.     <com.example.activity.IconTextView   
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/smile2"  
  18.         android:textSize="24dp"  
  19.         mobile:iconSrc="@drawable/smile"/>  
  20.       
  21.     <com.example.activity.IconTextView   
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="@string/smile3"  
  25.         android:textSize="36dp"  
  26.         mobile:iconSrc="@drawable/smile"/>  
  27.       
  28.   
  29. </LinearLayout>  

(3)在代码中使用context.obtainStyledAttributes获得属性值

[java] view plaincopy
  1. package com.example.activity;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Rect;  
  9. import android.util.AttributeSet;  
  10. import android.widget.TextView;  
  11.   
  12. public class IconTextView extends TextView {  
  13.     //命名空间  
  14.     private final String namespace = "http://com.example.activity";  
  15.     //资源ID  
  16.     private int resourceID = 0;  
  17.     private Bitmap bitmap;  
  18.   
  19.     public IconTextView(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);  
  22.         resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);  
  23.         if(resourceID > 0) {  
  24.             bitmap = BitmapFactory.decodeResource(getResources(), resourceID);  
  25.         }  
  26.     }  
  27.       
  28.     @Override  
  29.     public void onDraw(Canvas canvas) {  
  30.         if (bitmap != null) {  
  31.             Rect src = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  32.               
  33.             Rect target = new Rect();  
  34.             int textHeight = (int)getTextSize();  
  35.             target.left = 0;  
  36.             target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;  
  37.             target.bottom = target.top + textHeight;  
  38.             target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight()));  
  39.               
  40.             canvas.drawBitmap(bitmap, src, target, getPaint());  
  41.               
  42.             canvas.translate(target.right + 20);  
  43.         }  
  44.           
  45.         super.onDraw(canvas);  
  46.     }  
  47.       
  48. }  

第三种方法实例实现的是一个自定义的带图片的TextView,效果图如下

0 0
原创粉丝点击