自定义View 实现 TextView 的功能:

转载自:http://www.eoeandroid.com/thread-58501-1-1.html

 1.  定义Text2View 所用关键字"text" 用于指定显示用的string
* 在 res/value 目录下创建 attrs.xml 文件 如下定义:

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="Text2View">
  4. <attr name="text" format="string" />
  5. </declare-styleable>
  6. </resources>

2. 定义 public class Text2View extends View

 

  1. public class Text2View extends View {
  2. Paint text2Paint;
  3. String text2Text;
  4. int ascent;
  5. //Constructor - for java
  6. public Text2View(Context context) {
  7. super(context);
  8. initialize();
  9. }
  10. //Constructor - for xml
  11. public Text2View(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. initialize();
  14. TypedArray ta = context.obtainStyledAttributes(attrs,
  15. R.styleable.Text2View);
  16. int n = ta.getIndexCount();
  17. for(int i =0;i < n;i++){
  18. int attr = ta.getIndex(i);
  19. switch(attr){
  20. case R.styleable.Text2View_text:
  21. updateText(ta.getString(R.styleable.Text2View_text));
  22. break;
  23. //TO ADD CUSTOM ATTRIBUTE
  24. default:
  25. break;
  26. }
  27. }
  28. ta.recycle();
  29. }
  30. private final void updateText(String s){
  31. text2Text = s;
  32. requestLayout();
  33. invalidate();
  34. }
  35. //load default setting on Text2View
  36. private final void initialize() {
  37. text2Paint = new Paint();
  38. text2Paint.setAntiAlias(true);
  39. text2Paint.setTextSize(16);
  40. text2Paint.setColor(0xFF000000);
  41. //setPadding(3, 3, 3, 3);
  42. }
  43. @Override
  44. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  45. setMeasuredDimension(measureWidth(widthMeasureSpec),
  46. measureHeight(heightMeasureSpec));
  47. }
  48. private int measureWidth(int measureSpec) {
  49. int result = 0;
  50. int specMode = MeasureSpec.getMode(measureSpec);
  51. int specSize = MeasureSpec.getSize(measureSpec);
  52. if (specMode == MeasureSpec.EXACTLY) {
  53. // We were told how big to be
  54. result = specSize;
  55. }
  56. else {
  57. // Measure the text
  58. result = (int) text2Paint.measureText(text2Text) + getPaddingLeft()
  59. + getPaddingRight();
  60. if (specMode == MeasureSpec.AT_MOST) {
  61. // Respect AT_MOST value if that was what is called for by measureSpec
  62. result = Math.min(result, specSize);
  63. }
  64. }
  65. return result;
  66. }
  67. /**
  68. * Determines the height of this view
  69. * @param measureSpec A measureSpec packed into an int
  70. * @return The height of the view, honoring constraints from measureSpec
  71. */
  72. private int measureHeight(int measureSpec) {
  73. int result = 0;
  74. int specMode = MeasureSpec.getMode(measureSpec);
  75. int specSize = MeasureSpec.getSize(measureSpec);
  76. ascent = (int) text2Paint.ascent();
  77. if (specMode == MeasureSpec.EXACTLY) {
  78. // We were told how big to be
  79. result = specSize;
  80. } else {
  81. // Measure the text (beware: ascent is a negative number)
  82. result = (int) (-ascent + text2Paint.descent()) + getPaddingTop()
  83. + getPaddingBottom();
  84. if (specMode == MeasureSpec.AT_MOST) {
  85. // Respect AT_MOST value if that was what is called for by measureSpec
  86. result = Math.min(result, specSize);
  87. }
  88. }
  89. return result;
  90. }
  91. @Override
  92. protected void onDraw(Canvas canvas) {
  93. super.onDraw(canvas);
  94. canvas.drawText(text2Text, getPaddingLeft(), getPaddingTop() - ascent, text2Paint);
  95. }
  96. }
复制代码

3. Text2View 构造函数需要解释一下:

Java代码:

  1. //Constructor - for java
  2. public Text2View(Context context) {
  3. super(context);
  4. initialize();
  5. }
  6. //Constructor - for xml
  7. public Text2View(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. initialize();
  10. TypedArray ta = context.obtainStyledAttributes(attrs,
  11. R.styleable.Text2View);
  12. int n = ta.getIndexCount();
  13. for(int i =0;i < n;i++){
  14. int attr = ta.getIndex(i);
  15. switch(attr){
  16. case R.styleable.Text2View_text:
  17. updateText(ta.getString(R.styleable.Text2View_text));
  18. break;
  19. //TO ADD CUSTOM ATTRIBUTE
  20. default:
  21. break;
  22. }
  23. }
  24. ta.recycle();
  25. }
复制代码

4. 在 xml 文件中如何使用Text2View

Xml代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:<SPAN style="COLOR: #ff0000">app</SPAN>="http://schemas.android.com/apk/res/<SPAN style="COLOR: #ff0000">com.android.View</SPAN>"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content">
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="HelloTextView" />
  11. <SPAN style="COLOR: #888888"><<SPAN style="COLOR: #ff0000">com.android.View.Text2View</SPAN>
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. <SPAN style="COLOR: #ff0000">app</SPAN>:text="HelloText2View" /></SPAN>
  15. </LinearLayout>  

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值