大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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>

复制代码

当然上面的布局方式可以帮助我们完成简单应用的开发了,但是如果你想写一个复杂的应用,这样就有点牵强了,大家不信可以下源码都研究看看,高手写的布局方式,如上面的布局高手通常是这样写的:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <A>
  3. <B></B>
  4. </A>

复制代码

其中A extends LinerLayout, B extends TextView.

为了帮助大家更容易理解,我写了一个简单的Demo ,具体步骤如下:
首先新建一个Android 工程 命名为ViewDemo .
然后自定义一个View 类,命名为MyView(extends View) .代码如下:
  1. package com.android.tutor;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7. import android.graphics.Paint.Style;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. public class MyView extends View {
  11. private Paint mPaint;
  12. private Context mContext;
  13. private static final String mString = "Welcome to Mr Wei's blog";
  14. public MyView(Context context) {
  15. super(context);
  16. }
  17. public MyView(Context context,AttributeSet attr)
  18. {
  19. super(context,attr);
  20. }
  21. @Override
  22. protected void onDraw(Canvas canvas) {
  23. // TODO Auto-generated method stub
  24. super.onDraw(canvas);
  25. mPaint = new Paint();
  26. //设置画笔颜色
  27. mPaint.setColor(Color.RED);
  28. //设置填充
  29. mPaint.setStyle(Style.FILL);
  30. //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  31. canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
  32. mPaint.setColor(Color.BLUE);
  33. //绘制文字
  34. canvas.drawText(mString, 10, 110, mPaint);
  35. }
  36. }

复制代码

然后将我们自定义的View 加入到main.xml 布局文件中,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <com.android.tutor.MyView
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. />
  16. </LinearLayout>

复制代码

最后执行之,效果如下图:

android开发:Android 中自定义View的应用的更多相关文章

  1. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

  2. Android开发艺术探索笔记——View(二)

    Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...

  3. Android开发艺术探索笔记—— View(一)

    Android开发艺术探索笔记 --View(一) View的基础知识 什么是View View是Android中所有控件的基类.是一种界面层控件的抽象. View的位置参数 参数名 获取方式 含义 ...

  4. Android开发——Android多进程以及使用场景介绍

    个层级,具体可以查看Android开发--Android进程保活招式大全中1.1部分的内容,这里就不赘述了. 根据进程中当前活动组件的重要程度,Android 会将进程评定为它可能达到的最高级别.例如 ...

  5. Android开发-Android Studio问题以及解决记录

    [Android开发] Android Studio问题以及解决记录   http://blog.csdn.net/niubitianping/article/details/51400721 1.真 ...

  6. Android开发——Android M(6.0) 权限解决方案

    Android开发--Android M(6.0) 权限解决方案 自从Android M(6.0)发布以来,权限管理相比以前有了很大的改变,很多程序员发现之前运行的好好的Android应用在Andro ...

  7. Android Studio开发基础之自定义View组件

    一般情况下,不直接使用View和ViewGroup类,而是使用使用其子类.例如要显示一张图片可以用View类的子类ImageView,开发自定义View组件可分为两个主要步骤: 一.创建一个继承自an ...

  8. android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处

    自己定义一个view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  9. Android中自定义View的MeasureSpec使用

    有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custo ...

随机推荐

  1. Are you looking forward to this 11s Black Stingray

    The Derek Jeter Air Jordan 11 Navy Suede has quietly dropped a number of various colorways over the ...

  2. mongodb mongotemplate聚合

    1.group by并且计算总数 @Test public void insertTest() { //测试数据 //insertTestData(); Aggregation agg = Aggre ...

  3. zw版【转发·台湾nvp系列Delphi例程】HALCON SetComprise2

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetComprise2 procedure TForm1.Button1Click(Sender: TObject);var op : H ...

  4. Java设计模式应用——观察者模式

    告警结果产生后,可能需要发送短信,邮件,故障管理系统.这些转发操作不应当影响告警生成入库,并且类似事件可能根据不同场景,客户习惯不同,此时,使用观察者模式则可以很好的适应上述场景. 观察者模式应当包括 ...

  5. Python的问题解决: IOError: [Errno 32] Broken pipe

    被该问题困扰的人还是挺多的,所以又对这个问题进行了一些更深入的分析,希望可以解决读者的问题新版本:Python 的 Broken Pipe 错误问题分析 遇到一个很奇怪的问题, web.py代码里面报 ...

  6. 常用linux命令:locate 命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...

  7. python 采坑总结 调用键盘事件后导致键盘失灵的可能原因

    在练习python封装键盘事件的时候,实现一个keyDown和keyUp的功能: @staticmethod    def keyDown(keyName):        #按下按键        ...

  8. 禁止火狐浏览器缓存input标签方法

    禁止火狐浏览器缓存input标签方法 问题1:在火狐浏览器里,云平台的输入框.选项框.勾选框…填写之后按F5刷新页面,之前填的东西会保留着,其它浏览器不会火狐强制刷新用Ctrl + F5 浏览器自动保 ...

  9. 依赖注入(DI)在PHP中的实现

    什么是依赖注入? IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection,简称DI). 当一个类的实例需要另一 ...

  10. 20145304 Exp7 网络欺诈技术防范

    20145304 Exp7 网络欺诈技术防范 实验后回答问题 1.通常在什么场景下容易受到DNS spoof攻击 在公共网络下,如一些购物场所.咖啡馆.快餐店等提供的网络下:当自己常使用的无线网被有恶 ...