1. 程式人生 > >Android自定義View 自定義xml屬性設定

Android自定義View 自定義xml屬性設定

Android自定義View實現很簡單

繼承View,重寫建構函式、onDraw,(onMeasure)等函式。

如果自定義的View需要有自定義的屬性,需要在values下建立attrs.xml。在其中定義你的屬性。

在使用到自定義View的xml佈局檔案中需要加入xmlns:字首="http://schemas.android.com/apk/res/Android專案所在的包路徑即R檔案所在的包注意:不是自定義view包路徑)". 配置好後按Alt加 / 鍵在使用自定義屬性的時候沒提示自定義屬性程式碼,重啟eclipse可以解決,本人親測。或者寫成:xmlns:my="http://schemas.android.com/apk/res-auto"不過我覺得這個靠譜多了》

在使用自定義屬性的時候,使用字首:屬性名,如my:textColor="#FFFFFFF"。

例項:

  1. package demo.view.my;   
  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.Paint.Style;   
  8. import android.util.AttributeSet;   
  9. import android.view.View;   
  10. /**  
  11.  * 這個是自定義的TextView.  
  12.  * 至少需要過載構造方法和onDraw方法  
  13.  * 對於自定義的View如果沒有自己獨特的屬性,可以直接在xml檔案中使用就可以了  
  14.  * 如果含有自己獨特的屬性,那麼就需要在建構函式中獲取屬性檔案attrs.xml中自定義屬性的名稱  
  15.  * 並根據需要設定預設值,放在在xml檔案中沒有定義。  
  16.  * 如果使用自定義屬性,那麼在應用xml檔案中需要加上新的schemas,  
  17.  * 比如這裡是xmlns:my="http://schemas.android.com/apk/res/demo.view.my" 
     
  18.  * 其中xmlns後的“my”是自定義的屬性的字首,res後的是我們自定義View所在的包  
  19.  * @author Administrator  
  20.  *  
  21.  */
  22. publicclass MyView extends View {   
  23.     Paint mPaint; //畫筆,包含了畫幾何圖形、文字等的樣式和顏色資訊 
  24. public MyView(Context context) {   
  25. super(context);   
  26.     }   
  27. public MyView(Context context, AttributeSet attrs){   
  28. super(context, attrs);   
  29.         mPaint = new Paint();   
  30. //TypedArray是一個用來存放由context.obtainStyledAttributes獲得的屬性的陣列 
  31. //在使用完成後,一定要呼叫recycle方法 
  32. //屬性的名稱是styleable中的名稱+“_”+屬性名稱 
  33.         TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);   
  34. int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供預設值,放置未指定 
  35. float textSize = array.getDimension(R.styleable.MyView_textSize, 36);   
  36.         mPaint.setColor(textColor);   
  37.         mPaint.setTextSize(textSize);   
  38.         array.recycle(); //一定要呼叫,否則這次的設定會對下次的使用造成影響 
  39.     }   
  40. publicvoid onDraw(Canvas canvas){   
  41. super.onDraw(canvas);   
  42. //Canvas中含有很多畫圖的介面,利用這些介面,我們可以畫出我們想要的圖形 
  43. //mPaint = new Paint(); 
  44. //mPaint.setColor(Color.RED); 
  45.         mPaint.setStyle(Style.FILL); //設定填充 
  46.         canvas.drawRect(1010100100, mPaint); //繪製矩形 
  47.         mPaint.setColor(Color.BLUE);   
  48.         canvas.drawText("我是被畫出來的"10120, mPaint);   
  49.     }   
  50. }  

相應的屬性檔案:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="MyView">
  4. <attrname="textColor"format="color"/>
  5. <attrname="textSize"format="dimension"/>
  6. </declare-styleable>
  7. </resources>

在佈局檔案中的使用:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <demo.view.my.MyView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. my:textColor="#FFFFFFFF"
  12. my:textSize="22dp"
  13. />
  14. </LinearLayout>