自定义View自定义属性

第一步,在res/values文件在下自定义属性文件attrs:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="mytitleText" format="string" />
    <attr name="myTextColor" format="color" />
    <attr name="myTextSize" format="dimension" />
    <attr name="backColor" format="color" />

    <declare-styleable name="MyView"><span style="color:#ff0000;">//此处的name是你的自定义View的名称</span>
        <attr name="mytitleText" />
        <attr name="myTextColor" />
        <attr name="myTextSize" />
        <attr name="backColor" />
    </declare-styleable>
</resources>
第二步:在布局文件中引入自定义的view

加入命名空间

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bwie.zhaoning.customview.MainActivity">

    <com.bwie.zhaoning.customview.myview.MyView
        android:layout_height="200dp"
        android:layout_width="200dp"
        custom:mytitleText="hahaha"
        custom:myTextColor="#f00"
        custom:myTextSize="50sp"
        custom:backColor="#0f0"
        android:layout_centerInParent="true"
        android:id="@+id/myview"
        ></com.bwie.zhaoning.customview.myview.MyView>
</RelativeLayout>

自定义View如下:

package com.bwie.zhaoning.customview.myview;

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 com.bwie.zhaoning.customview.R;

/**
 * Created by dell-pc on 2016/8/4.
 */
public class MyView extends View {

    private Paint p;
    private String mTitleText;
    private int mTitleTextColor;
    private int mTitleTextSize;
    private Rect rect;
    private int mBackColor;
    //提供set方法可以在代码中设置属性
    public void setmTitleText(String mTitleText) {
        this.mTitleText = mTitleText;
        invalidate();
    }

    public void setmTitleTextColor(int mTitleTextColor) {
        this.mTitleTextColor = mTitleTextColor;
        invalidate();
    }

    public void setmTitleTextSize(int mTitleTextSize) {
        this.mTitleTextSize = mTitleTextSize;
        invalidate();
    }

    public void setmBackColor(int mBackColor) {
        this.mBackColor = mBackColor;
        invalidate();
    }

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {

        this(context,attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for(int i=0;i<n;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.MyView_mytitleText:
                    mTitleText = a.getString(attr);
                    break;
                case R.styleable.MyView_myTextColor:
                    // 默认颜色设置为黑色
                    mTitleTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MyView_myTextSize:
                    // 默认设置为16sp,TypeValue也可以把sp转化为px
                    mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.MyView_backColor:
                    // 默认颜色设置为黑色
                    mBackColor = a.getColor(attr, Color.BLACK);
                    break;
            }
        }
        p = new Paint();
        p.setColor(Color.BLUE);
        rect = new Rect();
        p.getTextBounds(mTitleText,0,mTitleText.length(),rect);
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        p.setColor(mBackColor);
        canvas.drawRect(0, 0,getMeasuredWidth(),getMeasuredHeight(), p);
        p.setColor(mTitleTextColor);
        p.setTextSize(mTitleTextSize);
        canvas.drawText(mTitleText, getWidth() / 2 - rect.width() / 2, getHeight() / 2 + rect.height() / 2,p);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int 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
        {
           p.setTextSize(mTitleTextSize);
            p.getTextBounds(mTitleText, 0, mTitleText.length(), rect);
            float textWidth = rect.width();
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY)
        {
            height = heightSize;
        } else
        {
            p.setTextSize(mTitleTextSize);
            p.getTextBounds(mTitleText, 0, mTitleText.length(), rect);
            float textHeight = rect.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }



        setMeasuredDimension(width, height);
    }
}


 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值