Android 自定义TextView

package com.lixinyang.myviews.Views;

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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lixinyang.myviews.MainActivity;
import com.lixinyang.myviews.R;

/**
 * 自定textview
 * 显示内容
 * 字体的大小
 * 字体的颜色
 * 控件的宽高
 * 1.自定属性
 * 1.在values文件夹下面创建一个attrs.xml文件
 * 2.定义我们需要的属性
 * 2.wrap_content  hello
 * 3.测量尺寸
 */
public class MyTextView extends View {
    private Paint paint;
    private int color;
    private float size;
    private String content;
    private Rect rect;
    private int aa;

    public MyTextView(Context context) {
        this(context,null);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取到你控件的属性并形成数组
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView, defStyleAttr, 0);
         //取出里面的属性
        content = ta.getString(R.styleable.MyTextView_content);
        color = ta.getColor(R.styleable.MyTextView_myColor, Color.BLUE);
        size = ta.getDimension(R.styleable.MyTextView_mySize, 40);
        //初始化画笔
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(size);
        rect = new Rect();
        //第一个参数放置你的文本
        // 第二个参数是你字符串开始的下标
        //第三个参数是你要字符串的长度
        //第四个参数返回给调用者
        paint.getTextBounds(content,0,content.length(), rect);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        aa = MainActivity.aa();
        canvas.drawText(content,0, aa,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取测量的模式和测量的尺寸
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        //判断测量模式并将你的控件大小改变
        if(widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }else{
            int width1 = rect.width();
            width=width1+getPaddingLeft()+getPaddingRight();
        }
        if(heightMode==MeasureSpec.EXACTLY){
            height=heightSize;
        }else{
            int height1 = rect.height();
            height = height1+getPaddingTop()+getPaddingBottom();
        }
        //将屏幕宽高度从新赋值
        setMeasuredDimension(width,height);

        //Toast.makeText(getContext(),""+aa, Toast.LENGTH_LONG).show();
    }
}
 
  
布局文件
 
  
xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:MyAttr="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lixinyang.myviews.MainActivity">

    <com.lixinyang.myviews.Views.MyTextView
        MyAttr:content="Hellow LixinYang"
        MyAttr:myColor="#f00"
        MyAttr:mySize="25sp"
        android:background="#00f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

LinearLayout>
XML文件
 
  
xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="content" format="string">attr>
    <attr name="myColor" format="color">attr>
    <attr name="mySize" format="dimension">attr>

    <declare-styleable name="MyTextView">
        <attr name="content">attr>
        <attr name="myColor">attr>
        <attr name="mySize">attr>
    declare-styleable>
resources>
MainActivity类
 
  
package com.lixinyang.myviews;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

public class MainActivity extends AppCompatActivity {

    private static Window window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        window = getWindow();

    }
    public static int aa(){
        Rect frame = new Rect();
        window.getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        int contentTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        //statusBarHeight是上面状态栏的高度
        int titleBarHeight = contentTop - statusBarHeight;
        //Toast.makeText(MainActivity.this,""+titleBarHeight,Toast.LENGTH_LONG).show();
        return titleBarHeight;
    }
}

你可能感兴趣的:(Android 自定义TextView)