第一步,在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);
}
}
”