大家好我们今天的教程是在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~

更多相关文章

  1. Android中图像变换Matrix的原理、代码验证和应用(一)
  2. Android第十五课 Jni自带的iconv库不支持GBK转码
  3. JAVA数据结构及算法--Android中Activity的四种启动模式
  4. Android学习笔记之MENU
  5. Android中图像变换Matrix的原理、代码验证和应用(一)
  6. Android系列之Android开发教程代码实例
  7. Android中几种图像特效处理
  8. Android(安卓)Service生命周期及用法!
  9. Android高手进阶教程(三)之----Android(安卓)中自定义View的应用

随机推荐

  1. Android中四种实现点击事件的方法
  2. Android_GPS定位
  3. How to create jar for Android Library
  4. Android-开源工具库-第6弹-Android(安卓)
  5. Unsupported major.minor version 52.0
  6. No private recovery resources for TARG
  7. Android中fill_parent、match_parent和wr
  8. Android ListView等getView调用多次问题
  9. Android 获得屏幕宽高的三种方式
  10. Android 球形进度条,动态控制时间与最大