大家好我们今天的教程是在Android教程中自定义View的学习,对于初学着来说,他们习惯了Android传统的页面布局方式,如下代码:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
当然上面的布局方式可以帮助我们完成简单应用的开发了,但是如果你想写一个复杂的应用,这样就有点牵强了,大家不信可以下源码都研究看看,高手写的布局方式,如上面的布局高手通常是这样写的:
- <?xmlversion="1.0"encoding="utf-8"?>
- <A>
- <B></B>
- </A>
- 其中AextendsLinerLayout,BextendsTextView.
为了帮助大家更容易理解,我写了一个简单的Demo ,具体步骤如下:
首先新建一个Android 工程 命名为ViewDemo .
然后自定义一个View 类,命名为MyView(extends View) .代码如下:
- packagecom.android.tutor;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.graphics.Rect;
- importandroid.graphics.Paint.Style;
- importandroid.util.AttributeSet;
- importandroid.view.View;
- publicclassMyViewextendsView{
- privatePaintmPaint;
- privateContextmContext;
- privatestaticfinalStringmString="WelcometoMrWei'sblog";
- publicMyView(Contextcontext){
- super(context);
- }
- publicMyView(Contextcontext,AttributeSetattr)
- {
- super(context,attr);
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- super.onDraw(canvas);
- mPaint=newPaint();
- mPaint.setColor(Color.RED);
- mPaint.setStyle(Style.FILL);
- canvas.drawRect(newRect(10,10,100,100),mPaint);
- mPaint.setColor(Color.BLUE);
- canvas.drawText(mString,10,110,mPaint);
- }
- }
然后将我们自定义的View 加入到main.xml 布局文件中,代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <com.android.tutor.MyView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
最后执行之,效果如下图:
OK,大功告成,今天就写到这里,开始做饭了,老婆孩子等我做饭了,lol~
分享到:
相关推荐
Android高手进阶教程之----Android 中自定义View的应用.doc Android高手进阶教程之----Android 中自定义属性(attr.xml,TypedArray)的使用! .doc Android高手进阶教程之----Android中万能的BaseAdapter(Spinner,...
《HenCoder Android 开发进阶:自定义 View 1-5 绘制顺序》 的练习项目
自UI工作原理:Viewgroup/View/Window/WindowManager之间关系 Android事件分发机制 View重要属性和方法 自定义控件实现
在我们从Android小学生 进阶到 Android中学生的路上,肯定需要经历 自定义View。 大神已经给出了,最精简的Demo,我这里 顺水推舟,把一些基础知识 标记在代码之中,各位可以一边看源码 一边学基础。 觉得文章有用,...
我的Android进阶之旅------>Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能 http://blog.csdn.net/ouyang_peng/article/details/50813419 1、实现歌词同步滚动的功能,即歌曲播放到哪句...
自定义android进度条,带有进度指示,项目详情:http://blog.csdn.net/xiaanming/article/details/10298163
Android高级进阶五部曲,主要包含 android高级进阶Kotlin实战篇 Android高级进阶NDK实战篇 Android高级进阶自定义View篇 Android高级进阶系统架构篇 Android高级进阶项目实战篇
NULL 博文链接:https://hz-chenwenbiao-91.iteye.com/blog/2043677
三、Android开发工具集 在Android开发过程中,选择合适的工具能够大大提高开发效率。因此,我们整理了常用的Android开发工具集,包括开发工具、测试工具、性能优化工具等。这些工具都是经过我们精心筛选和测试的,...
本书是一本Android进阶类书籍,书中各知识点由浅入深、环环相扣,最终这些知识点形成了一个体系结构。本书共分为11章。第1章介绍Android 5.0到Android 7.0的新特性。第2章介绍Material Design。第3章介绍View体系,...
资源为博客示例代码:http://blog.csdn.net/lmj623565791/article/details/24300125 有问题请留言
此作为Android自定义View之画圆环(进阶篇:圆形进度条)的示例代码,如有疑问,请留言
Android自定义View实现开关按钮效果,适用于Android进阶初级开发者参考探讨。博文地址:http://blog.csdn.net/jaynm/article/details/52601935
资源为博客实例:http://blog.csdn.net/lmj623565791/article/details/24529807 有问题博客留言
用于做Path动画的自定义View。 I have a path.I have a view. (Oh~),Path(Anim)View. 现已经找到图片->SVG->PATH的正确姿势, Now i have a pic.I have a view. Oh~,Path(Anim)View. 相关博文: 实现详解: ...
Android研究院之应用程序自定义View(二) Android研究院之应用程序系统控件界面(三) Android研究院之应用程序TextView详解(四) Android研究院之应用程序EditText 详解(五) Android研究院之应用程序ListView ...
很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义View上面花一些功夫,多写一些文章。先总结下自定义View的步骤: 1、自定义View的属性 2、...
Android自定义控件一般有三种方式 1、继承Android固有的控件,在Android原生控件的基础上,进行添加功能和逻辑。 2、继承ViewGroup,这类自定义控件是可以往自己的布局里面添加其他的子控件的。 3、继承View,这类...