自定義控件3.自定義屬性

在上一篇裏, 自定義View的背景色和文字顏色都是在 onDraw 裏寫死的, 此次咱們實現能夠經過佈局文件配置. 要實現這個功能, 須要有以下幾個步驟:

1. 建立 attrs.xml

在 res/values 目錄下新建一個 attrs.xml , 內容以下:
   
   
   
   
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- declare CustomView2 style --> <declare-styleable name="CustomView2"> <attr name="textColor" format="color" /> <attr name="backgroundColor" format="color" /> </declare-styleable></resources>

2. 在佈局文件中使用

    
    
    
    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res/com.gaoyuan4122.customui" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.gaoyuan4122.customui.views.CustomView1 android:id="@+id/cv_custom1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_hello" /> <TextView android:id="@+id/tv_hello2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_below="@id/cv_custom1" /> <com.gaoyuan4122.customui.views.CustomView2 android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_hello2" app:backgroundColor="#ff00ff" app:textColor="#ffff00" /></RelativeLayout>
注意:
1. 要在最外層的控件中加一個新的名稱空間, 好比上面叫 xmlns:app="...", 這裏的名字能夠隨便取.
2. 在控件上使用的時候, 要加上命名空間, 注意和上面定義的保持一致.

3. 在自定義 View 中使用

自定義屬性須要在構造方法中獲取, 上一篇文章裏說了, 繼承 View 須要實現兩個構造方法, 一個是隻有 Context 參數的, 一個是有 Context, AttributeSet 參數的, 其實自定義 View 還有一個構造方法, 有三個參數: Context, AttributeSet, int(defStyleAttr), 第二個參數 AttributeSet 封裝了View的屬性信息, 咱們自定義的屬性會被封裝到這個類裏, 第三個參數和樣式有關, 這裏先不說. View的三個構造方法通常是下面的寫法:
    
    
    
    
  1. // 自定義屬性會被封裝到 attrs 中, 第三個參數和樣式有關, 這裏先不說.
private int mTextColor; private int mBackgroundColor; public CustomView2(Context context) { this(context, null); } public CustomView2(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomView2(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // 經過下面的方法去除自定義屬性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView2); mTextColor = typedArray.getColor(R.styleable.CustomView2_textColor, Color.RED); mBackgroundColor = typedArray.getColor(R.styleable.CustomView2_backgroundColor, Color.BLUE); // 最後必定要注意調用這個方法, 至於爲何後面會說. typedArray.recycle(); }
經過上面的方式能夠將自定義屬性取出來, 而後就能夠使用了:
    
    
    
    
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setStyle(Style.FILL); mPaint.setColor(mBackgroundColor); mRect.set(0, 0, 100, 100); canvas.drawRect(mRect, mPaint); mPaint.setColor(mTextColor); mPaint.setTextSize(30.0f); canvas.drawText("Hello!", 0, 100, mPaint); }
注意別忘了重寫 onMeasure 方法, 不然又會出現 View 的寬高爲 match_parent.
最後的效果以下:
 

4. 關於自定義屬性的 format

自定義屬性的時候, <attrs> 標籤中有一個 format 屬性, 這個屬性值表示將用戶傳入的值以何種方式進行解析, 好比解析成一個字符串, 或者一個 color 值等等, 下面列舉了全部能夠解析的類型:
1. reference:參考某一資源ID。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "background" format = "reference" />

            </declare-styleable>

    (2)屬性使用:

             <ImageView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/圖片ID"

                     />

2. color:顏色值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "textColor" format = "color" />

            </declare-styleable>

    (2)屬性使用:

            <TextView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:textColor = "#00FF00"

                     />

3. boolean:布爾值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "focusable" format = "boolean" />

            </declare-styleable>

    (2)屬性使用:

            <Button

                    android:layout_width = "42dip"
                    android:layout_height = "42dip"

                    android:focusable = "true"

                    />

4. dimension:尺寸值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "layout_width" format = "dimension" />

            </declare-styleable>

    (2)屬性使用:

            <Button

                    android:layout_width = "42dip"
                    android:layout_height = "42dip"

                    />

5. float:浮點值。

    (1)屬性定義:

            <declare-styleable name = "AlphaAnimation">

                   <attr name = "fromAlpha" format = "float" />
                   <attr name = "toAlpha" format = "float" />

            </declare-styleable>

    (2)屬性使用:

            <alpha
                   android:fromAlpha = "1.0"
                   android:toAlpha = "0.7"

                   />

6. integer:整型值。

    (1)屬性定義:

            <declare-styleable name = "AnimatedRotateDrawable">

                   <attr name = "visible" />
                   <attr name = "frameDuration" format="integer" />
                   <attr name = "framesCount" format="integer" />
                   <attr name = "pivotX" />
                   <attr name = "pivotY" />
                   <attr name = "drawable" />

            </declare-styleable>

    (2)屬性使用:

            <animated-rotate

                   xmlns:android = "http://schemas.android.com/apk/res/android"  
                   android:drawable = "@drawable/圖片ID"  
                   android:pivotX = "50%"  
                   android:pivotY = "50%"  
                   android:framesCount = "12"  
                   android:frameDuration = "100"

                   />

7. string:字符串。

    (1)屬性定義:

            <declare-styleable name = "MapView">
                   <attr name = "apiKey" format = "string" />
            </declare-styleable>

    (2)屬性使用:

            <com.google.android.maps.MapView
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

                    />

8. fraction:百分數。

    (1)屬性定義:

            <declare-styleable name="RotateDrawable">
                   <attr name = "visible" />
                   <attr name = "fromDegrees" format = "float" />
                   <attr name = "toDegrees" format = "float" />
                   <attr name = "pivotX" format = "fraction" />
                   <attr name = "pivotY" format = "fraction" />
                   <attr name = "drawable" />
            </declare-styleable>

    (2)屬性使用:

            <rotate

                   xmlns:android = "http://schemas.android.com/apk/res/android" 
               android:interpolator = "@anim/動畫ID"

                   android:fromDegrees = "0" 
               android:toDegrees = "360"

                   android:pivotX = "200%"

                   android:pivotY = "300%" 
               android:duration = "5000"

                   android:repeatMode = "restart"

                   android:repeatCount = "infinite"

                   />

9. enum:枚舉值。

    (1)屬性定義:

            <declare-styleable name="名稱">
                   <attr name="orientation">
                          <enum name="horizontal" value="0" />
                          <enum name="vertical" value="1" />
                   </attr>            

            </declare-styleable>

    (2)屬性使用:

            <LinearLayout

                    xmlns:android = "http://schemas.android.com/apk/res/android"
                    android:orientation = "vertical"
                    android:layout_width = "fill_parent"
                    android:layout_height = "fill_parent"
                    >
            </LinearLayout>

10. flag:位或運算。

     (1)屬性定義:

             <declare-styleable name="名稱">
                    <attr name="windowSoftInputMode">
                            <flag name = "stateUnspecified" value = "0" />
                            <flag name = "stateUnchanged" value = "1" />
                            <flag name = "stateHidden" value = "2" />
                            <flag name = "stateAlwaysHidden" value = "3" />
                            <flag name = "stateVisible" value = "4" />
                            <flag name = "stateAlwaysVisible" value = "5" />
                            <flag name = "adjustUnspecified" value = "0x00" />
                            <flag name = "adjustResize" value = "0x10" />
                            <flag name = "adjustPan" value = "0x20" />
                            <flag name = "adjustNothing" value = "0x30" />
                     </attr>         

             </declare-styleable>

     (2)屬性使用:

            <activity

                   android:name = ".StyleAndThemeActivity"
                   android:label = "@string/app_name"
                   android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
                   <intent-filter>
                          <action android:name = "android.intent.action.MAIN" />
                          <category android:name = "android.intent.category.LAUNCHER" />
                   </intent-filter>
             </activity>

     注意:

     屬性定義時能夠指定多種類型值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "background" format = "reference|color" />

            </declare-styleable>

    (2)屬性使用:

             <ImageView

                     android:layout_width = "42dip"
                     android:layout_height = "42dip"
                     android:background = "@drawable/圖片ID|#00FF00"

                     />


相關文章
相關標籤/搜索