Android高手进阶教程(三)之----Android 中自定义View的应用. 2010-04-18 21:11:25标签:Android进阶View定义教程原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。
否则将追究法律责任。
/1556324/311457大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码:view plaincopy to clipboardprint?1.<?xml version="1.0" encoding="utf-8"?>2.<LinearLayout xmlns:android="/apk/res/android"3. android:orientation="vertical"4. android:layout_width="fill_parent"5. android:layout_height="fill_parent"6. >7.<TextView8. android:layout_width="fill_parent"9. android:layout_height="wrap_content"10. android:text="@string/hello"11. />12.</LinearLayout>13.<?xml version="1.0" encoding="utf-8"?>14.<LinearLayout xmlns:android="/apk/res/android"15. android:orientation="vertical"16. android:layout_width="fill_parent"17. android:layout_height="fill_parent"18. >19.<TextView20. android:layout_width="fill_parent"21. android:layout_height="wrap_content"22. android:text="@string/hello"23. />24.</LinearLayout>当然上面的布局方式可以帮助我们完成简单应用的开发了,但是如果你想写一个复杂的应用,这样就有点牵强了,大家不信可以下源码都研究看看,高手写的布局方式,如上面的布局高手通常是这样写的:view plaincopy to clipboardprint?1.<?xml version="1.0" encoding="utf-8"?>2.<A>3. <B></B>4.</A>5.<?xml version="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.view plaincopy to clipboardprint?2.package com.android.tutor;3.import android.content.Context;4.import android.graphics.Canvas;5.import android.graphics.Color;6.import android.graphics.Paint;7.import android.graphics.Rect;8.import android.graphics.Paint.Style;9.import android.util.AttributeSet;10.i mport android.view.View;11.p ublic class MyView extends View {12. private Paint mPaint;13. private Context mContext;;15.16. public MyView(Context context) {17. super(context);18.19. }20. public MyView(Context context,AttributeSet attr)21. {22. super(context,attr);23.24. }25. @Override26. protected void onDraw(Canvas canvas) {27. // TODO Auto-generated method stub28. super.onDraw(canvas);29.30. mPaint = new Paint();31.32. //设置画笔颜色33. mPaint.setColor(Color.RED);34. //设置填充35. mPaint.setStyle(Style.FILL);36.37. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标38. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);39.40. mPaint.setColor(Color.BLUE);41. //绘制文字42. canvas.drawText(mString, 10, 110, mPaint);43. }44.}45.p ackage com.android.tutor;46.i mport android.content.Context;47.i mport android.graphics.Canvas;48.i mport android.graphics.Color;49.i mport android.graphics.Paint;50.i mport android.graphics.Rect;51.i mport android.graphics.Paint.Style;52.i mport android.util.AttributeSet;53.i mport android.view.View;54.p ublic class MyView extends View {55.private Paint mPaint;56.private Context mContext;58.59.public MyView(Context context) {60. super(context);61.62.}63.public MyView(Context context,AttributeSet attr)64.{65. super(context,attr);66.67.}68.@Override69.protected void onDraw(Canvas canvas) {70. // TODO Auto-generated method stub71. super.onDraw(canvas);72.73. mPaint = new Paint();74.75. //设置画笔颜色76. mPaint.setColor(Color.RED);77. //设置填充78. mPaint.setStyle(Style.FILL);79.80. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标81. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);82.83. mPaint.setColor(Color.BLUE);84. //绘制文字85. canvas.drawText(mString, 10, 110, mPaint);86.}87.}88.89.然后将我们自定义的View 加入到main.xml 布局文件中,代码如下:90.v iew plaincopy to clipboardprint?91.<?xml version="1.0" encoding="utf-8"?>92.<LinearLayout xmlns:android="/apk/res/android"93. android:orientation="vertical"94. android:layout_width="fill_parent"95. android:layout_height="fill_parent"96. >97.<TextView98. android:layout_width="fill_parent"99. android:layout_height="wrap_content"100. android:text="@string/hello"101. />102.<com.android.tutor.MyView103. android:layout_width="fill_parent"104. android:layout_height="fill_parent"105./>106.</LinearLayout>107.<?xml version="1.0" encoding="utf-8"?>108.<LinearLayout xmlns:android="/apk /res/android"109. android:orientation="vertical"110. android:layout_width="fill_parent"111. android:layout_height="fill_parent"112. >113.<TextView114. android:layout_width="fill_parent"115. android:layout_height="wrap_content"116. android:text="@string/hello"117. />118.<com.android.tutor.MyView119. android:layout_width="fill_parent"120. android:layout_height="fill_parent"121./>122.</LinearLayout>最后执行之,效果如下图:OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~对于Android系统的自定义View可能大家都熟悉了,对于自定义View的属性添加,以及Android的Layout的命名空间问题,很多网友还不是很清楚,今天Android123一起再带大家温习一下。