Android高手進階教程(四)之----Android 中自定義屬性(attr.xml,TypedArray)的使用!

原文來自:http://blog.csdn.net/Android_Tutor/article/details/5508615


今天我們的教程是根據前面一節擴展進行的,如果你沒有看,請點擊 Android高手進階教程(三) 查看第三課,這樣跟容易方便你的理解!

 

xml 文件裏定義控件的屬性,我們已經習慣了android:attrs="" ,那麼我們能不能定義自己的屬性能,比如:test:attrs="" 呢?答案是肯定的.

 

好了我就不賣關子了,直接進入主題。大致以下步驟:

 

一、 在res/values 文件下定義一個attrs.xml 文件.代碼如下:

 

 

[java] view plaincopy
  1. 一、在res/values文件下定義一個attrs.xml文件.代碼如下:  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <resources>  
  4.     <declare-styleable name="MyView">  
  5.         <attr name="textColor" format="color" />  
  6.         <attr name="textSize" format="dimension" />  
  7.     </declare-styleable>  
  8. </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() 方法,爲了保持以後使用該屬性一致性!

 

[java] view plaincopy
  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 全部代碼如下:

 

[java] view plaincopy
  1. package com.android.tutor;  
  2. import android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Rect;  
  8. import android.graphics.Paint.Style;  
  9. import android.util.AttributeSet;  
  10. import android.view.View;  
  11. public class MyView extends View {  
  12.     private Paint mPaint;  
  13.     private Context mContext;  
  14.     private static final String mString = "Welcome to Mr Wei's blog";  
  15.       
  16.     public MyView(Context context) {  
  17.         super(context);  
  18.         mPaint = new Paint();  
  19.     }  
  20.     public MyView(Context context,AttributeSet attrs)  
  21.     {  
  22.         super(context,attrs);  
  23.         mPaint = new Paint();  
  24.           
  25.         TypedArray a = context.obtainStyledAttributes(attrs,  
  26.                 R.styleable.MyView);  
  27.           
  28.         int textColor = a.getColor(R.styleable.MyView_textColor,  
  29.                 0XFFFFFFFF);  
  30.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
  31.           
  32.         mPaint.setTextSize(textSize);  
  33.         mPaint.setColor(textColor);  
  34.           
  35.         a.recycle();  
  36.     }  
  37.     @Override  
  38.     protected void onDraw(Canvas canvas) {  
  39.         // TODO Auto-generated method stub  
  40.         super.onDraw(canvas);  
  41.         //設置填充  
  42.         mPaint.setStyle(Style.FILL);  
  43.           
  44.         //畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標  
  45.         canvas.drawRect(new Rect(1010100100), mPaint);  
  46.           
  47.         mPaint.setColor(Color.BLUE);  
  48.         //繪製文字  
  49.         canvas.drawText(mString, 10110, mPaint);  
  50.     }  
  51. }  

 

三、將我們自定義的MyView 加入佈局main.xml 文件中,平且使用自定義屬性,自定義屬性必須加上:

      xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor "藍色 是自定義屬性的前綴,紅色 是我們包名.

main.xml 全部代碼如下:

 

[java] view plaincopy
  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>  

 

四、運行之效果如下圖:

 

 

今天就到此結束,大家有什麼疑問的,請留言,我會及時答覆大家!謝謝~


發佈了5 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章