今天我們的教程是根據前面一節擴充進行的,如果你沒有看,請點擊 Android高手進階教程(三)
查看第三課,這樣跟容易方便你的理解!
在xml
檔案裡定義控制項的屬性,我們已經習慣了android:attrs=""
,那麼我們能不能定義自己的屬效能,比如:test:attrs=""
呢?答案是肯定的.
好了我就不賣關子了,直接進入主題。大致以下步驟:
一、
在res/values
檔案下定義一個attrs.xml
檔案.代碼如下:
一、在res/values檔案下定義一個attrs.xml檔案.代碼如下:<br /><?xml version="1.0" encoding="utf-8"?><br /><resources><br /> <declare-styleable name="MyView"><br /> <attr name="textColor" format="color" /><br /> <attr name="textSize" format="dimension" /><br /> </declare-styleable><br /></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()
方法,為了保持以後使用該屬性一致性!
public MyView(Context context,AttributeSet attrs)<br />{<br />super(context,attrs);<br />mPaint = new Paint();</p><p>TypedArray a = context.obtainStyledAttributes(attrs,<br />R.styleable.MyView);</p><p>int textColor = a.getColor(R.styleable.MyView_textColor,<br />0XFFFFFFFF);<br />float textSize = a.getDimension(R.styleable.MyView_textSize, 36);</p><p>mPaint.setTextSize(textSize);<br />mPaint.setColor(textColor);</p><p>a.recycle();<br />}
MyView.java
全部代碼如下:
package com.android.tutor;<br />import android.content.Context;<br />import android.content.res.TypedArray;<br />import android.graphics.Canvas;<br />import android.graphics.Color;<br />import android.graphics.Paint;<br />import android.graphics.Rect;<br />import android.graphics.Paint.Style;<br />import android.util.AttributeSet;<br />import android.view.View;<br />public class MyView extends View {<br />private Paint mPaint;<br />private Context mContext;<br />private static final String mString = "Welcome to Mr Wei's blog";</p><p>public MyView(Context context) {<br />super(context);<br />mPaint = new Paint();<br />}<br />public MyView(Context context,AttributeSet attrs)<br />{<br />super(context,attrs);<br />mPaint = new Paint();</p><p>TypedArray a = context.obtainStyledAttributes(attrs,<br />R.styleable.MyView);</p><p>int textColor = a.getColor(R.styleable.MyView_textColor,<br />0XFFFFFFFF);<br />float textSize = a.getDimension(R.styleable.MyView_textSize, 36);</p><p>mPaint.setTextSize(textSize);<br />mPaint.setColor(textColor);</p><p>a.recycle();<br />}<br />@Override<br />protected void onDraw(Canvas canvas) {<br />// TODO Auto-generated method stub<br />super.onDraw(canvas);<br />//設定填充<br />mPaint.setStyle(Style.FILL);</p><p>//畫一個矩形,前倆個是矩形左上方座標,後面倆個是右下角座標<br />canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);</p><p>mPaint.setColor(Color.BLUE);<br />//繪製文字<br />canvas.drawText(mString, 10, 110, mPaint);<br />}<br />}<br />
三、將我們自訂的MyView
加入布局main.xml
檔案中,平且使用自訂屬性,自訂屬性必須加上:
xmlns: #0000ff;">test
="http://schemas.android.com/apk/res/com.android.tutor
"藍色
是自訂屬性的首碼,#ff0000;">紅色
是我們包名.
main.xml
全部代碼如下:
<?xml<br />version="1.0" encoding="utf-8"?><br /><LinearLayout<br />xmlns:android="http://schemas.android.com/apk/res/android"</p><p>xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/hello"<br /> /><br /><com.android.tutor.MyView<br />android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> test:textSize="20px"<br /> test:textColor="#fff"<br />/><br /></LinearLayout><br />
四、運行之效果如:
今天就到此結束,大家有什麼疑問的,請留言,我會及時回覆大家!謝謝~