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"這種格式,否則會出錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM