標籤:android自訂view
Q1:為什麼要自訂view?A:由於很多系統內建的view滿足不了當前設計需求或者為了達到更良好的使用者體驗,增加UI的美化效果,就需要自定viewQ2:自訂view有那幾個步驟?A:>使用者可根據需要extends View這個父類,然後重寫父類的方法;如:onDraw();onMeasure()等; >如果使用者在自訂View事需要添加屬性,則必須在values檔案夾下建立"attrs.xml"檔案,在其中添加自訂屬性。 下面來進行自訂view的學習。一、最基本的自訂view;例如:建立一個類CustomEditText 繼承EditText其特點:擁有EditText的全部方法,就和EditText一模一樣,只是名字不同了而已。使用方法有兩種:>在代碼中調用,CustomEditText cet=new CustomEditText(context);>在xml中調用,需要寫全包名及類名,否則會報The following classes could not be found;關鍵代碼如下:自訂view:public class CustomEditText extends EditText {public CustomEditText(Context context) {super(context);// TODO Auto-generated constructor stub}public CustomEditText(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public CustomEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}}xml中使用:<! -- com.anqiansong.views為CustomEditText所在位置的包名--><com.anqiansong.views.CustomEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自訂EditText" />
二、在上面的CustomEditText中重寫onDraw()方法實現刪除線text效果;@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint=new Paint();//建立畫筆對象paint.setColor(Color.BLACK);//設定畫筆顏色paint.setAntiAlias(true);//設定是否消除鋸齒paint.setStrokeWidth(2);//設定畫筆的粗細//設定線條的x軸方向的起始點,y軸方向的起始點canvas.drawLine(this.getLeft(), (this.getBottom()-this.getTop())/2, this.getRight(), (this.getBottom()-this.getTop())/2, paint);}
三、比如我們現在在xml中使用時添加一個lineColor屬性(刪除線的顏色),該怎麼實現呢?>首先需要在values下建立attrs.xml檔案;>然後根據需要添加相應的屬性及屬性所屬類型>使用關鍵代碼:<?xml version="1.0" encoding="utf-8"?><resources> <!--declare-styleable:聲明樣式類型;attr name=""聲明屬性名稱;format="屬性的類型" --> <declare-styleable name="CustomEditText"> <attr name="lineColor" format="color" /> </declare-styleable></resources>備忘:format的屬性值見*附錄一在xml中調用:以下代碼必須聲明,否則找不到之前定義的屬性名稱,則不能用,可以聲明在根布局中,也可以在調用時聲明xmlns:CustomEditText="http://schemas.android.com/apk/res/com.anqiansong.androidcustomview"(如果沒有聲明,在引用屬性時系統也會提示然後根據提示自動產生將是已xmlns:app=.....這種模式)說明:xmlns:***="http://schemas.android.com/apk/res/packagename"***主要是在調用時需要,如android:textColor="#ffffff";這裡的“android”方式1中的CustomEditText:lineColor="#ff0000"的CustomEditText,方式2中的mylinecolor而packagename則為當前工程包名如果這兩個不是這種格式,則也引用不了自訂的屬性此外,既然在xml中設定的lineColor的值,在自訂view中的構造方法中就要去擷取這個屬性的顏色值;如代碼:public class CustomEditText extends EditText {private int lineColor;//刪除線顏色全域變數public CustomEditText(Context context) {super(context);// TODO Auto-generated constructor stub}public CustomEditText(Context context, AttributeSet attrs) {super(context, attrs);Paint paint=new Paint();TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText);lineColor=array.getColor(R.styleable.CustomEditText_lineColor, Color.BLACK);//擷取xml中設定的刪除線的顏色值,這裡的CustomEditText_lineColor是由attrs中的styleable中的名字和屬性名稱加"_"組合而成array.recycle();}public CustomEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint=new Paint();//建立畫筆對象paint.setColor(lineColor);//設定畫筆顏色paint.setAntiAlias(true);//設定是否消除鋸齒paint.setStrokeWidth(2);//設定畫筆的粗細//設定線條的x軸方向的起始點,y軸方向的起始點canvas.drawLine(this.getLeft(), (this.getBottom()-this.getTop())/2, this.getRight(), (this.getBottom()-this.getTop())/2, paint);}}方式1:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:CustomEditText="http://schemas.android.com/apk/res/com.anqiansong.androidcustomview" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.anqiansong.views.CustomEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自訂EditText" CustomEditText:lineColor="#ff0000" /></RelativeLayout>方式2:<com.anqiansong.views.CustomEditText xmlns:mylinecolor="http://schemas.android.com/apk/res/com.anqiansong.androidcustomview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自訂EditText" mylinecolor:lineColor="#ff0000" />同樣的原理,我們可以對刪除線的粗細添加一個屬性lineHeight,代碼如下:添加屬性:<attr name="lineHeight" format="dimension"/>引用屬性:mylinecolor:lineHeight="5dp"自訂view中擷取lineHeight並設定粗細:lineHeight=array.getDimension(R.styleable.CustomEditText_lineHeight, 2);在ondraw()方法中paint.setStrokeWidth(lineHeight);//設定畫筆的粗細
附錄一:附:Android中自訂屬性的格式詳解(引自:http://www.cnblogs.com/zhangs1986/p/3243040.html,歡醉,請尊重原創)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" />
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android自訂view(初級篇)