这里自定义一个TextView extends View
private String mText; private int mColor = Color.BLACK; private int mTextSize = 15;
1,构造方法
// 初始化 new 的时候使用// TextView tv = new TextView(this); public TextView(Context context) { super(context); } //在布局layout中使用(调用) /* <com.example.administrator.mytextview.TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> */ public TextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } /* 在布局layout中使用(调用),但是会用style * <com.example.administrator.mytextview.TextView style="@style/define_style" android:text="Hello World!" /> * * */ public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
2,复写自定义view的测量方法 onMeasure();
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //布局的宽高,用这个方法去指定 //控件(TextView)的宽高,需要测量 //获取宽高模式.wrap_content,match_parent,100dp int widthMode = MeasureSpec.getMode(widthMeasureSpec); //2位的测量模式模式 int HeightMode = MeasureSpec.getMode(heightMeasureSpec); int widthsize = MeasureSpec.getSize(widthMeasureSpec); //30位。某种测量模式下的规格大小 //获取模式后,if判断 if (widthMode == MeasureSpec.AT_MOST){ //MeasureSpec.AT_MOST在布局中指定的是wrap_content,将获取它 } else if (widthMode == MeasureSpec.EXACTLY){//MeasureSpec.EXACTLY在布局中指定的是 确切的值 或者 match_parent,将获取它 } else { //widthMode == MeasureSpec.UNSPECIFIED (尽可能的大),很少能用到. listView,scrollview,在测量子布局的时候会用。 } }3,复写onDraw方法
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // canvas.drawBitmap(); //画文本 // canvas.drawCircle(); //画文本 // canvas.drawText(); //画弧 // canvas.drawArc(); }
4,onTouchEvent方法,处理和用户的交互
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_UP: // 抬起 break; case MotionEvent.ACTION_DOWN: //按下 break; case MotionEvent.ACTION_MOVE: //移动 break; } return super.onTouchEvent(event); //交给父类处理 } }
5,自定义属性:
res/values/new--->ValuesResourceFile/attrs.xml在attrs.xml中:<?xml version="1.0" encoding="utf-8"?> <resources> <!--name = 自定义view的名字--> <declare-styleable name="TextView"> <!--name = 自定义属性的名称(任意) format 格式--> <attr name="text" format="string"/> <attr name="textColor" format="color"/> <!--dimension:尺寸; 用于 宽高,字体大小....--> <attr name="textSize" format="dimension"/> <attr name="maxLength" format="integer"/> <!--reference:资源(drawable)--> <attr name="background" format="reference|color"/> <!--枚举--> <attr name="inputType" > <enum name = "number" value="1"/> <enum name = "text" value="2"/> <enum name = "password" value="3"/> </attr> </declare-styleable> </resources>
2)java中,具体实现。获取属性:1>先修改这几个构造函数:public TextView(Context context) { this(context,null); }
public TextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); }public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }使得每次都调用第三个构造函数.public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr);//获取自定义的属性TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TextView); mText = array.getString(R.styleable.TextView_text); mColor = array.getColor(R.styleable.TextView_textColor,mColor); mTextSize = array.getInteger(R.styleable.TextView_textSize,mTextSize); //回收释放 array.recycle();
}
2>
3)布局中使用时先引入命名空间,但是一般都默认引用了。
使用:app:text = "hello world!"