自定义TextView,在values下面新建attrs.xml,布局,重写方法

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值