Q1:为什么要自定义view? A:由于很多系统自带的view满足不了当前设计需求或者为了达到更良好的用户体验,增加UI的美化效果,就需要自定view Q2:自定义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效果; @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.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 } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.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" />
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-12 03:30:50