大家好我们今天的教程是在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>
最后执行之,效果如下图: Android高手进阶教程(三)之----Android 中自定义View的应用. OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~

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

更多相关文章

  1. Android 布局 LinearLayout与RelativeLayout的布局属性
  2. android 相对定位布局方向
  3. android studio开发 控件布局
  4. android 布局属性
  5. AndroidStudio 基础控件与布局
  6. Android布局属性一览表
  7. android布局属性详解分享
  8. RelativeLayout(相对布局)
  9. Android五大布局

随机推荐

  1. android移动数据上网的开关的实现
  2. 腾讯微博客户端开发视频--若水(第三集、第
  3. Android SDK 快速安装方法
  4. android获取APK签名信息及MD5指纹
  5. 【面包屑】快速使用RecyclerView搭建列表
  6. 修改ListView的分割线
  7. Android之Handler详解(四)
  8. Android LayoutInflater
  9. eclipse导入的Android项目没有android.ja
  10. Android面试系列文章2018之Android部分Ha