Android 中自訂控制項和屬性(attr.xml,declare-styleable,TypedArray)的方法和使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   使用   java   

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

Android 中自訂控制項和屬性(attr.xml,declare-styleable,TypedArray)的方法和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.