values下面的attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomTextView"> <attr name="text" format="string"/> <attr name="textSize" format="dimension"/> <attr name="textColor" format="color"/> </declare-styleable> </resources> activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.menglucywhh.myapplicationday1031.view.ZiDingYiTextView android:layout_width="100dp" android:layout_height="100dp" app:text="haha" app:textColor="@color/colorAccent" app:textSize="20sp" /> </LinearLayout> package com.example.menglucywhh.myapplicationday1031.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; import android.widget.TextView; import com.example.menglucywhh.myapplicationday1031.R; public class ZiDingYiTextView extends TextView{ private String text; private int color; private int textSize; private Paint mpaint; private Rect mRect; //重写三个方法,一个参数,两个参数,桑格参数 public ZiDingYiTextView(Context context) { super(context); } //两个参数的 public ZiDingYiTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); //有几种属性.count int count = typedArray.getIndexCount(); //遍历循环 for (int i=0;i<count;i++){ int attr = typedArray.getIndex(i); //判断是哪个属性 switch (attr){ case R.styleable.CustomTextView_text: text = typedArray.getString(attr); break; case R.styleable.CustomTextView_textColor: color = typedArray.getInt(attr, Color.BLUE); break; case R.styleable.CustomTextView_textSize: //DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); textSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics())); break; } } //回收 typedArray.recycle(); //new一个画笔 mpaint = new Paint(); //给画笔设置粗细 mpaint.setTextSize(textSize); mRect = new Rect(); mpaint.getTextBounds(text,0,text.length(), mRect); } public ZiDingYiTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); System.out.println("w = " + w + " " + h + " " + oldw + " " + oldh); } @Override //测量大小的方法 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); System.out.println("width = " + width); System.out.println("height = " + height); //setMeasuredDimension(100,100); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); int widthMRect = mRect.width(); int heightMRect = mRect.height(); mpaint.setColor(Color.BLUE); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mpaint); mpaint.setColor(color); canvas.drawText(text,(getWidth()-mRect.width())/2,(getHeight()+mRect.height())/2,mpaint); } }
自定义TextView,在values下面新建attrs.xml,布局,重写方法
最新推荐文章于 2022-09-21 22:45:23 发布