Android 自定义View
2010-09-27 02:04:00 来源:WEB开发网在values/attrs.xml中:
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< declare-styleable name="MyView">
< attr name="textColor" format="color" />
< attr name="textSize" format="dimension" />
< attr name="imgBackground" format="integer" />
< attr name="textPaddingLeft" format="dimension"/>
< attr name="textPaddingTop" format="dimension"/>
< /declare-styleable>
< /resources>
编写MyView.java,继承View
package test.cuntomizedview;
import java.util.Calendar;
import test.cuntomizedview.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint mPaint;
private Context mContext;
private String mStr;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initMyView();
TypedArray params = context.obtainStyledAttributes(attrs,
R.styleable.MyView);
int backgroudId = params.getResourceId(
R.styleable.MyView_imgBackground, 0);
if (backgroudId != 0)
setBackgroundResource(backgroudId);
int textColor = params.getColor(R.styleable.MyView_textColor,
0XFFFFFFFF);
setTextColor(textColor);
float textSize = params.getDimension(R.styleable.MyView_textSize, 36);
setTextSize(textSize);
float paddingLeft = params.getDimension(
R.styleable.MyView_textPaddingLeft, 41);
更多精彩
赞助商链接