大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: view plaincopy to clipboardprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. </LinearLayout>
  13. <?xmlversion="1.0"encoding="utf-8"?>
  14. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  15. android:orientation="vertical"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. >
  19. <TextView
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="@string/hello"
  23. />
  24. </LinearLayout>
当然上面的布局方式可以帮助我们完成简单应用的开发了,但是如果你想写一个复杂的应用,这样就有点牵强了,大家不信可以下源码都研究看看,高手写的布局方式,如上面的布局高手通常是这样写的: view plaincopy to clipboardprint?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <A>
  3. <B></B>
  4. </A>
  5. <?xmlversion="1.0"encoding="utf-8"?>
  6. <A>
  7. <B></B>
  8. </A>
view plaincopy to clipboardprint?
其中A extends LinerLayout, B extends TextView.
其中A extends LinerLayout, B extends TextView. 为了帮助大家更容易理解,我写了一个简单的Demo ,具体步骤如下: 首先新建一个Android 工程 命名为ViewDemo . 然后自定义一个View 类,命名为MyView(extends View) .代码如下:
  1. viewplaincopytoclipboardprint?
  2. packagecom.android.tutor;
  3. importandroid.content.Context;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.graphics.Rect;
  8. importandroid.graphics.Paint.Style;
  9. importandroid.util.AttributeSet;
  10. importandroid.view.View;
  11. publicclassMyViewextendsView{
  12. privatePaintmPaint;
  13. privateContextmContext;
  14. privatestaticfinalStringmString="WelcometoMrWei'sblog";
  15. publicMyView(Contextcontext){
  16. super(context);
  17. }
  18. publicMyView(Contextcontext,AttributeSetattr)
  19. {
  20. super(context,attr);
  21. }
  22. @Override
  23. protectedvoidonDraw(Canvascanvas){
  24. //TODOAuto-generatedmethodstub
  25. super.onDraw(canvas);
  26. mPaint=newPaint();
  27. //设置画笔颜色
  28. mPaint.setColor(Color.RED);
  29. //设置填充
  30. mPaint.setStyle(Style.FILL);
  31. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  32. canvas.drawRect(newRect(10,10,100,100),mPaint);
  33. mPaint.setColor(Color.BLUE);
  34. //绘制文字
  35. canvas.drawText(mString,10,110,mPaint);
  36. }
  37. }
  38. packagecom.android.tutor;
  39. importandroid.content.Context;
  40. importandroid.graphics.Canvas;
  41. importandroid.graphics.Color;
  42. importandroid.graphics.Paint;
  43. importandroid.graphics.Rect;
  44. importandroid.graphics.Paint.Style;
  45. importandroid.util.AttributeSet;
  46. importandroid.view.View;
  47. publicclassMyViewextendsView{
  48. privatePaintmPaint;
  49. privateContextmContext;
  50. privatestaticfinalStringmString="WelcometoMrWei'sblog";
  51. publicMyView(Contextcontext){
  52. super(context);
  53. }
  54. publicMyView(Contextcontext,AttributeSetattr)
  55. {
  56. super(context,attr);
  57. }
  58. @Override
  59. protectedvoidonDraw(Canvascanvas){
  60. //TODOAuto-generatedmethodstub
  61. super.onDraw(canvas);
  62. mPaint=newPaint();
  63. //设置画笔颜色
  64. mPaint.setColor(Color.RED);
  65. //设置填充
  66. mPaint.setStyle(Style.FILL);
  67. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  68. canvas.drawRect(newRect(10,10,100,100),mPaint);
  69. mPaint.setColor(Color.BLUE);
  70. //绘制文字
  71. canvas.drawText(mString,10,110,mPaint);
  72. }
  73. }
  74. 然后将我们自定义的View加入到main.xml布局文件中,代码如下:
  75. viewplaincopytoclipboardprint?
  76. <?xmlversion="1.0"encoding="utf-8"?>
  77. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  78. android:orientation="vertical"
  79. android:layout_width="fill_parent"
  80. android:layout_height="fill_parent"
  81. >
  82. <TextView
  83. android:layout_width="fill_parent"
  84. android:layout_height="wrap_content"
  85. android:text="@string/hello"
  86. />
  87. <com.android.tutor.MyView
  88. android:layout_width="fill_parent"
  89. android:layout_height="fill_parent"
  90. />
  91. </LinearLayout>
  92. <?xmlversion="1.0"encoding="utf-8"?>
  93. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  94. android:orientation="vertical"
  95. android:layout_width="fill_parent"
  96. android:layout_height="fill_parent"
  97. >
  98. <TextView
  99. android:layout_width="fill_parent"
  100. android:layout_height="wrap_content"
  101. android:text="@string/hello"
  102. />
  103. <com.android.tutor.MyView
  104. android:layout_width="fill_parent"
  105. android:layout_height="fill_parent"
  106. />
  107. </LinearLayout>
最后执行之,效果如下图: OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~

本文出自 “Android_Tutor” 博客,请务必保留此出处http://weizhulin.blog.51cto.com/1556324/311457

更多相关文章

  1. Android截取开机关机事件
  2. Android入门篇一:Android(安卓)Activity生命周期
  3. Android调用本机地图APP
  4. Android之 drawTextOnpath
  5. Android:自动完成文本框
  6. Android高手进阶教程(二十)之---Android与JavaScript方法相互调
  7. android坐标系相关知识点
  8. ListActivity setContentView 错误
  9. Android中使用HttpURLConnection和HttpClient发送Http请求

随机推荐

  1. Javascript实现统一的表单验证
  2. 从特定条件下存储在localStorage中的数组
  3. 停止鼠标用javascript双击某些元素
  4. 在量角器中检索子元素的数组
  5. Node.js无法找到模块'tcp'
  6. jQuery和AJAX - 使用Ajax添加的对象动态
  7. JavaScript学习-5——异步同步、回调函数
  8. Micorosft Edge - 嵌入式PDF - 如何打
  9. AngularJS ng-repeat over multiple tr
  10. javascript小技巧&&JavaScript[对象.属性