转载自:http://www.eoeandroid.com/thread-58501-1-1.html
1. 定义Text2View 所用关键字"text" 用于指定显示用的string
* 在 res/value 目录下创建 attrs.xml 文件 如下定义:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="Text2View">
- <attr name="text" format="string" />
- </declare-styleable>
- </resources>
2. 定义 public class Text2View extends View
- public class Text2View extends View {
- Paint text2Paint;
- String text2Text;
- int ascent;
- //Constructor - for java
- public Text2View(Context context) {
- super(context);
- initialize();
- }
- //Constructor - for xml
- public Text2View(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize();
- TypedArray ta = context.obtainStyledAttributes(attrs,
- R.styleable.Text2View);
- int n = ta.getIndexCount();
- for(int i =0;i < n;i++){
- int attr = ta.getIndex(i);
- switch(attr){
- case R.styleable.Text2View_text:
- updateText(ta.getString(R.styleable.Text2View_text));
- break;
- //TO ADD CUSTOM ATTRIBUTE
- default:
- break;
- }
- }
- ta.recycle();
- }
- private final void updateText(String s){
- text2Text = s;
- requestLayout();
- invalidate();
- }
- //load default setting on Text2View
- private final void initialize() {
- text2Paint = new Paint();
- text2Paint.setAntiAlias(true);
- text2Paint.setTextSize(16);
- text2Paint.setColor(0xFF000000);
- //setPadding(3, 3, 3, 3);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(measureWidth(widthMeasureSpec),
- measureHeight(heightMeasureSpec));
- }
- private int measureWidth(int measureSpec) {
- int result = 0;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- if (specMode == MeasureSpec.EXACTLY) {
- // We were told how big to be
- result = specSize;
- }
- else {
- // Measure the text
- result = (int) text2Paint.measureText(text2Text) + getPaddingLeft()
- + getPaddingRight();
- if (specMode == MeasureSpec.AT_MOST) {
- // Respect AT_MOST value if that was what is called for by measureSpec
- result = Math.min(result, specSize);
- }
- }
- return result;
- }
- /**
- * Determines the height of this view
- * @param measureSpec A measureSpec packed into an int
- * @return The height of the view, honoring constraints from measureSpec
- */
- private int measureHeight(int measureSpec) {
- int result = 0;
- int specMode = MeasureSpec.getMode(measureSpec);
- int specSize = MeasureSpec.getSize(measureSpec);
- ascent = (int) text2Paint.ascent();
- if (specMode == MeasureSpec.EXACTLY) {
- // We were told how big to be
- result = specSize;
- } else {
- // Measure the text (beware: ascent is a negative number)
- result = (int) (-ascent + text2Paint.descent()) + getPaddingTop()
- + getPaddingBottom();
- if (specMode == MeasureSpec.AT_MOST) {
- // Respect AT_MOST value if that was what is called for by measureSpec
- result = Math.min(result, specSize);
- }
- }
- return result;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- canvas.drawText(text2Text, getPaddingLeft(), getPaddingTop() - ascent, text2Paint);
- }
- }
3. Text2View 构造函数需要解释一下:
Java代码:
- //Constructor - for java
- public Text2View(Context context) {
- super(context);
- initialize();
- }
- //Constructor - for xml
- public Text2View(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize();
- TypedArray ta = context.obtainStyledAttributes(attrs,
- R.styleable.Text2View);
- int n = ta.getIndexCount();
- for(int i =0;i < n;i++){
- int attr = ta.getIndex(i);
- switch(attr){
- case R.styleable.Text2View_text:
- updateText(ta.getString(R.styleable.Text2View_text));
- break;
- //TO ADD CUSTOM ATTRIBUTE
- default:
- break;
- }
- }
- ta.recycle();
- }
4. 在 xml 文件中如何使用Text2View
Xml代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:<SPAN style="COLOR: #ff0000">app</SPAN>="http://schemas.android.com/apk/res/<SPAN style="COLOR: #ff0000">com.android.View</SPAN>"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="HelloTextView" />
- <SPAN style="COLOR: #888888"><<SPAN style="COLOR: #ff0000">com.android.View.Text2View</SPAN>
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- <SPAN style="COLOR: #ff0000">app</SPAN>:text="HelloText2View" /></SPAN>
- </LinearLayout>