如何自定义一个简单的View.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr format="string" name="text"/>
<attr format="dimension" name="textsize"/>
<attr format="color" name="backGround"/>
<declare-styleable name="MyTextView">
<attr name="text"/>
<attr name="textsize"/>
<attr name="backGround"/>
</declare-styleable>
</resources>
其中我自定义了 text textsize backgroud 三个属性 ,format 的作用为定义属性值的类型有String color blooean float,intager,dimension等几种类型
declare-styleable 的作用: styleale 的出现系统可以为我们完成很多常量(int[]数组,下标常量)等的编写,简化我们的开发工作
第二步构造函数继承View 重写OnMeasure();OnDrow();方法;
//在上面构造方法里面我们获得我们自定义的样式;public class MyTextView extends View {
// 定以属性
private String mText;
private int mBackground;
private int mTextSize;
private Rect mBunds;
private Paint mPaint;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, com.example.asasas.R.styleable.MyTextView,
defStyleAttr, 0);
for (int i = 0; i < a.getIndexCount(); i++) {
int array = a.getIndex(i);
switch (array) {
case R.styleable.MyTextView_text:
mText = a.getString(array);
break;
case R.styleable.MyTextView_textsize:
mTextSize = a.getDimensionPixelSize(array, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
16, getResources().getDisplayMetrics()));
break;
sp与dp的互换
case R.styleable.MyTextView_backGround:mBackground = a.getColor(array, Color.BLACK);
break;
}
}
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mBunds = new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
}
TypeArray的作用:
TypedArray其实是用来简化我们的工作的,如果布局中的属性的值是引用类型(比如:@dimen/dp100),如果使用AttributeSet去获得最终的像素值,那么需要第一步拿到id,第二步再去解析id。而TypedArray正是帮我们简化了这个过程。
AttributeSet的作用
AttributeSet中保存的是该View声明的所有的属性以通过它去获取(自定义的)属性
重写OnMeasure()获得大小的大小@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stubint width;
int height;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = getPaddingLeft() + getPaddingRight() + widthSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingLeft() + mBunds.width() + getPaddingRight());
width = desired;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode == MeasureSpec.EXACTLY) {
height = getPaddingTop() + getPaddingBottom() + heightSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBunds);
int desired = (int) (getPaddingTop() + mBunds.height() + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
重写OnDrow();方法画出控件
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
mPaint.setColor(mBackground);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint = new Paint();
canvas.drawText(mText, getWidth() / 2 - mBunds.width() / 2, getHeight() / 2 + mBunds.height() / 2, mPaint);
}
第三步:在布局文件中使用自定义的控件.
<com.example.asasas.View.MyTextView
android:layout_height="43dp"
android:layout_width="32dp" app:textsize="26sp"
app:text="测试1"
app:backGround="#578"
android:padding="10dp"/>
<com.example.asasas.View.MyTextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:textsize="45dp" app:text="测试2"
app:backGround="#713"
android:padding="15dp"
android:layout_centerInParent="true"/>
更多详情:http://blog.csdn.net/lmj623565791/article/details/24252901
.