一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
然后在布局中声明我们的自定义View创建我们需要自定义的类| package tzq.customview.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.TypedValue; | |
| import android.view.View; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Random; | |
| import tzq.customview.R; | |
| public class MyCustomerView extends View { | |
| /** | |
| * 文本 | |
| */ | |
| PRivate String mText; | |
| /** | |
| * 文本颜色 | |
| */ | |
| private int mTextColor; | |
| /** | |
| * 文本字体大小 | |
| */ | |
| private int mTextSize; | |
| private Paint mPaint; | |
| private Rect mBound; | |
| public MyCustomerView(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| } | |
| public MyCustomerView(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| initView(context, attrs); | |
| } | |
| public MyCustomerView(Context context) { | |
| super(context); | |
| } | |
| private void initView(Context context, AttributeSet attrs) { | |
| TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.customstyle); | |
| int count = mTypedArray.getIndexCount(); | |
| for (int attr = 0; attr < count; attr++) { | |
| switch (attr) { | |
| case R.styleable.customstyle_text: | |
| mText = mTypedArray.getString(attr); | |
| break; | |
| case R.styleable.customstyle_textColor: | |
| mTextColor = mTypedArray.getColor(attr, Color.BLACK); | |
| break; | |
| case R.styleable.customstyle_textSize: | |
| mTextSize = mTypedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); | |
| break; | |
| } | |
| } | |
| mTypedArray.recycle(); | |
| mPaint = new Paint(); | |
| mPaint.setTextSize(mTextSize); | |
| mPaint.setAntiAlias(true); | |
| mBound = new Rect(); | |
| mPaint.getTextBounds(mText, 0, mText.length(), mBound); | |
| this.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| mText = createStringRadom(); | |
| postInvalidate(); | |
| } | |
| }); | |
| } | |
| private String createStringRadom() { | |
| Random mRandom = new Random(); | |
| List<Integer> listSize = new ArrayList<Integer>(); | |
| while (listSize.size() < 4) { | |
| listSize.add(mRandom.nextInt(10)); | |
| } | |
| StringBuilder mStringBuilder = new StringBuilder(); | |
| for (int value : listSize) { | |
| mStringBuilder.append(value); | |
| } | |
| return mStringBuilder.toString(); | |
| } | |
| @Override | |
| protected void onDraw(Canvas canvas) { | |
| super.onDraw(canvas); | |
| mPaint.setColor(Color.YELLOW); | |
| canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint); | |
| mPaint.setColor(mTextColor); | |
| canvas.drawText(mText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint); | |
| } | |
| @Override | |
| protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
| int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
| int widthModel = MeasureSpec.getMode(widthMeasureSpec); | |
| int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
| int heightModel = MeasureSpec.getMode(heightMeasureSpec); | |
| int width, height; | |
| if (widthModel == MeasureSpec.EXACTLY) { | |
| width = widthSize; | |
| } else { | |
| mPaint.setTextSize(mTextSize); | |
| mPaint.getTextBounds(mText, 0, mText.length(), mBound); | |
| float textWidth = mBound.width(); | |
| int desiredWith = (int) (getPaddingLeft() + getPaddingRight() + textWidth); | |
| width = desiredWith; | |
| } | |
| if (heightModel == MeasureSpec.EXACTLY) { | |
| height = heightSize; | |
| } else { | |
| mPaint.setTextSize(mTextSize); | |
| mPaint.getTextBounds(mText, 0, mText.length(), mBound); | |
| float textHeight = mBound.height(); | |
| int desiredHeight = (int) (getPaddingTop() + getPaddingBottom() + textHeight); | |
| height = desiredHeight; | |
| } | |
| setMeasuredDimension(width, height); | |
| } | |
| }如果需要制定view的大小需要重谢onmeasure方法 获取当前model 和sizwExACTLY 父类视图确定子类视图的大小由SpecSize 的值确定 对应Android:layoutWidth="";AT_MOST最大的值由父类specSize 确定,子类view 最大值不能大于父类确定值UNSPECIFIED 父用器不对view大小限制 |
.png)
新闻热点
疑难解答