Android自定义view

一、自定义属性
1、在values文件夹下新建attrs.xml文件
2、自定义属性name:名字,format:类型

<resources>
    <attr name="mText" format="string"></attr>
    <attr name="textSize" format="dimension"></attr>
    <attr name="textColor" format="color"></attr>
    <declare-styleable name="CustomText">
        <attr name="mText"></attr>
        <attr name="textSize"></attr>
        <attr name="textColor"></attr>
    </declare-styleable>
</resources>

3、在布局文件中使用自定义控件及属性
注意:要加入 xmlns:app=”http://schemas.android.com/apk/res-auto” 方可使用自定义属性。“app”可以替换成自己的命名。

<com.clkj.customerview.CustomText
        android:layout_width="300dp"
        android:layout_height="200dp"
        app:mText="1234"
        app:textColor="#ff0000"
        app:textSize="19dp"/>

二、自定义控件
1、新建CustomText类继承自View 具体代码如下
onDraw方法中进行绘制,onMeasure测量控件大小(本质是最终将测量得到的宽高值传递到setMeasuredDimension()中),onLayout方法进行布局(一般继承ViewGroup需要实现此方法来确定子控件的位置)

public class CustomText extends View {
    private Paint mPaint;
    private int count;
    private Rect mBounds;
    private String mText;
    private int color;
    private float size;

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

    public CustomText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomText, defStyleAttr, 0);//要使用obtainStyledAttributes四个参数的方法获得自定义属性
        int count = ta.getIndexCount();
        for(int i = 0;i<count;i++){
            int attr = ta.getIndex(i);
            switch (attr){
                case R.styleable.CustomText_mText:
                    mText = ta.getString(attr);
                    break;
                case R.styleable.CustomText_textColor:
                    color = ta.getColor(i,Color.BLUE);
                    break;
                case R.styleable.CustomText_textSize:
                    size = ta.getDimension(i,15);
                    break;
            }
        }
        ta.recycle();
        mPaint = new Paint();
        mBounds = new Rect();
        mPaint.getTextBounds(mText,0,mText.length(),mBounds);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.YELLOW);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
        mPaint.setColor(color);
        mPaint.setTextSize(size);
        canvas.drawText(mText,getMeasuredWidth()/2-mBounds.width()/2,getMeasuredHeight()/2+mBounds.height()/2,mPaint);
    }
}

@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;
        mPaint.setTextSize(size);
        mPaint.getTextBounds(mText,0,mText.length(),mBounds);
        if(widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else{

            int textWidth = mBounds.width();
            width = getPaddingLeft() + textWidth + getPaddingRight();
        }
        if(heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else{
            height = getPaddingTop() + mBounds.height() + getPaddingBottom();
        }
        setMeasuredDimension(width,height);
    }

你可能感兴趣的:(android)