TypedArray、attrs.xml、AttributeSet

ypedArray和attrs.xml和AttributeSet這一系列都是自定義控件屬性時要用到的內容。

首先說說attrs.xml:它是定義成類似於這種形式的。

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

1.主要講講裏面的format屬性:

 

. reference:參考某一資源ID。

(1)屬性定義:

  1. <declare-styleable name = "名稱"> 
  2.  
  3.                    <attr name = "background" format = "reference" /> 
  4.  
  5.             </declare-styleable> 

(2)屬性使用:

 

  1. <ImageView 
  2.       android:layout_width = "42dip" 
  3.       android:layout_height = "42dip" 
  4.       android:background = "@drawable/圖片ID" 
  5. /> 

②. color:顏色值。

(1)屬性定義:

  1. <declare-styleable name = "名稱"> 
  2.  
  3.                    <attr name = "textColor" format = "color" /> 
  4.  
  5.             </declare-styleable> 

(2)屬性使用:     

  1. <TextView 
  2.      android:layout_width = "42dip" 
  3.      android:layout_height = "42dip" 
  4.      android:textColor = "#00FF00"                     
  5.  /> 

③. boolean:布爾值。

    (1)屬性定義: 

  1. <declare-styleable name = "名稱"> 
  2.  
  3.                    <attr name = "focusable" format = "boolean" /> 
  4.  
  5.             </declare-styleable> 

    (2)屬性使用:

  1. <Button 
  2.      android:layout_width = "42dip" 
  3.      android:layout_height = "42dip" 
  4.      android:focusable = "true" 
  5. /> 

④. dimension:尺寸值。

    (1)屬性定義:  

  1. <declare-styleable name = "名稱"> 
  2.          <attr name = "layout_width" format = "dimension" /> 
  3. </declare-styleable> 

    (2)屬性使用:  

  1. <Button 
  2.      android:layout_width = "42dip" 
  3.      android:layout_height = "42dip" 
  4. /> 

⑤. float:浮點值。

    (1)屬性定義:        

  1. <declare-styleable name = "AlphaAnimation"> 
  2.  
  3.                    <attr name = "fromAlpha" format = "float" /> 
  4.                    <attr name = "toAlpha" format = "float" /> 
  5.  
  6.             </declare-styleable> 

    (2)屬性使用:           

  1. <alpha 
  2.    android:fromAlpha = "1.0" 
  3.    android:toAlpha = "0.7" 
  4.  /> 

⑥. integer:整型值。

    (1)屬性定義:     

  1. <declare-styleable name = "AnimatedRotateDrawable"> 
  2.  
  3.                    <attr name = "visible" /> 
  4.                    <attr name = "frameDuration" format="integer" /> 
  5.                    <attr name = "framesCount" format="integer" /> 
  6.                    <attr name = "pivotX" /> 
  7.                    <attr name = "pivotY" /> 
  8.                    <attr name = "drawable" /> 
  9.  
  10.             </declare-styleable> 

    (2)屬性使用:

         

  1. <animated-rotate 
  2.        xmlns:android = "http://schemas.android.com/apk/res/android"  
  3.        android:drawable = "@drawable/圖片ID"  
  4.        android:pivotX = "50%"  
  5.        android:pivotY = "50%"  
  6.        android:framesCount = "12"  
  7.        android:frameDuration = "100" 

⑦. string:字符串。

    (1)屬性定義:     

  1. <declare-styleable name = "MapView"> 
  2.                    <attr name = "apiKey" format = "string" /> 
  3.             </declare-styleable> 

    (2)屬性使用:  

  1. <com.google.android.maps.MapView 
  2.                    android:layout_width = "fill_parent" 
  3.                    android:layout_height = "fill_parent" 
  4.                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" 
  5.  
  6.                    /> 

⑧. fraction:百分數。

    (1)屬性定義:            

  1. <declare-styleable name="RotateDrawable"> 
  2.                    <attr name = "visible" /> 
  3.                    <attr name = "fromDegrees" format = "float" /> 
  4.                    <attr name = "toDegrees" format = "float" /> 
  5.                    <attr name = "pivotX" format = "fraction" /> 
  6.                    <attr name = "pivotY" format = "fraction" /> 
  7.                    <attr name = "drawable" /> 
  8.             </declare-styleable> 

    (2)屬性使用:  

  1. <rotate 
  2.  xmlns:android = "http://schemas.android.com/apk/res/android" 
  3.   android:interpolator = "@anim/動畫ID" 
  4.     android:fromDegrees = "0" 
  5.   android:toDegrees = "360" 
  6.     android:pivotX = "200%" 
  7.     android:pivotY = "300%" 
  8.    android:duration = "5000" 
  9.     android:repeatMode = "restart" 
  10.     android:repeatCount = "infinite" 
  11.  /> 

⑨. enum:枚舉值。

    (1)屬性定義:      

  1. <declare-styleable name="名稱"> 
  2.                    <attr name="orientation"> 
  3.                           <enum name="horizontal" value="0" /> 
  4.                           <enum name="vertical" value="1" /> 
  5.                    </attr>            
  6.  </declare-styleable> 

    (2)屬性使用:       

  1. <LinearLayout 
  2.  xmlns:android = "http://schemas.android.com/apk/res/android" 
  3.                     android:orientation = "vertical" 
  4.                     android:layout_width = "fill_parent" 
  5.                     android:layout_height = "fill_parent" 
  6. > 
  7. </LinearLayout> 

10. flag:位或運算。

     (1)屬性定義:         

  1. <declare-styleable name="名稱"> 
  2. <attr name="windowSoftInputMode"> 
  3.      <flag name = "stateUnspecified" value = "0" /> 
  4.      <flag name = "stateUnchanged" value = "1" /> 
  5.      <flag name = "stateHidden" value = "2" /> 
  6.      <flag name = "stateAlwaysHidden" value = "3" /> 
  7.      <flag name = "stateVisible" value = "4" /> 
  8.      <flag name = "stateAlwaysVisible" value = "5" /> 
  9.      <flag name = "adjustUnspecified" value = "0x00" /> 
  10.      <flag name = "adjustResize" value = "0x10" /> 
  11.      <flag name = "adjustPan" value = "0x20" /> 
  12.      <flag name = "adjustNothing" value = "0x30" /> 
  13. </attr>         
  14. </declare-styleable> 

     (2)屬性使用:

            

  1. <activity 
  2.       android:name = ".StyleAndThemeActivity" 
  3.       android:label = "@string/app_name" 
  4.       android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> 
  5.      <intent-filter> 
  6.         <action android:name = "android.intent.action.MAIN" /> 
  7.         <category android:name ="android.intent.category.LAUNCHER" /> 
  8.      </intent-filter> 
  9. </activity> 


注意:

屬性定義時可以指定多種類型值。

(1)屬性定義:

 

  1. <declare-styleable name = "名稱"> 
  2.       <attr name = "background" format = "reference|color" /> 
  3. </declare-styleable> 

(2)屬性使用:

 

  1. <ImageView 
  2.     android:layout_width = "42dip" 
  3.     android:layout_height = "42dip" 
  4.     android:background = "@drawable/圖片ID|#00FF00" 
  5. /> 

 

下面是一個佈局文件:

 

  1. <?xml  
  2. version="1.0" encoding="utf-8"?> 
  3. <LinearLayout  
  4. xmlns:android="http://schemas.android.com/apk/res/android" 
  5. //自定義的View的路徑爲com.xc.demo               
  6. xmlns:test="http://schemas.android.com/apk/res/com.xc.demo" 
  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.xc.demo.MyView 
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="fill_parent"  
  19. //這裏引用 改變字體和顏色 textColor和textSize都是根據前面的attrs.xml文件設置的
  20.     test:textSize="100dp" 
  21.     test:textColor="#ff0000" 
  22. /> 
  23. </LinearLayout> 

2.TypedArray的作用是在代碼中設置。

  1. public MyView(Context context, AttributeSet attrs) { 
  2.         this(context); 
  3.          
  4.         TypedArray a = context 
  5.                 .obtainStyledAttributes(attrs, R.styleable.MyView); 
  6.         int color = a.getColor(R.styleable.MyView_textColor, 0XFF0000FF); 
  7.         float size = a.getDimension(R.styleable.MyView_textSize, 50); 
  8.          
  9.         mPaint.setColor(color); 
  10.         mPaint.setTextSize(size); 
  11.          
  12.         a.recycle(); 
  13.     } 

R.styleable.MyView是attrs.xml中<declare-styleable name="MyView">的名字

具體的設置是依靠名字+"_"+名字得到來設置的。

3.如果代碼和xml中都設置了,一般以xml中設置爲先。

轉自:http://5200415.blog.51cto.com/3851969/1012267


/********************************************************************************************************************************/

Android使用AttributeSet自定義控件的方法

所謂自定義控件(或稱組件)也就是編寫自己的控件類型,而非Android中提供的標準的控件,如TextView,CheckBox等等.不過自定義的控件一般也都是從標準控件繼承來的,或者是多種控件組合,或者是對標準控件的屬性進行改變而得到的自己滿意的控件.

    自定義控件可能會有很多種方法,這裏只介紹我要介紹的方法.

 

    在這種方法中,大概的步驟是這樣的

    1.我們的自定義控件和其他的控件一樣,應該寫成一個類,而這個類的屬性是是有自己來決定的.

    2.我們要在res/values目錄下建立一個attrs.xml的文件,並在此文件中增加對控件的屬性的定義.

    3.使用AttributeSet來完成控件類的構造函數,並在構造函數中將自定義控件類中變量與attrs.xml中的屬性連接起來.

    4.在自定義控件類中使用這些已經連接的屬性變量.

    5.將自定義的控件類定義到佈局用的xml文件中去.

    6.在界面中生成此自定義控件類對象,並加以使用.

 

    好了,按照上述的方法,我們來看看http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

    博客中的實例代碼,按步驟加以解釋:

    //---------------------------------------------------------------------------------

    1. 定義自己的控件類:--------------------------------------------代碼1.

    package com.android.tutor;  
    import android.content.Context;  
    import android.content.res.TypedArray;  
    import android.graphics.Canvas;  
    import android.graphics.Color;  
    import android.graphics.Paint;  
    import android.graphics.Rect;  
    import android.graphics.Paint.Style;  
    import android.util.AttributeSet;  
    import android.view.View;  

 
    public class MyView extends View
    {  
        private Paint mPaint;  
        private Context mContext;  
        private static final String mString = "Welcome to Mr Wei's blog";  
      
        public MyView(Context context)
        {  
            super(context);  
            mPaint = new Paint();  
        }  

 
        public MyView(Context context,AttributeSet attrs)  
        {  
            super(context,attrs);  
            mPaint = new Paint();  
          
            TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);             
            int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
            float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
          
        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章