如何自定义自己的VIEW
来源:互联网 发布:淘宝店怎么做广告 编辑:程序博客网 时间:2024/05/29 04:47
1.继承VIew类
View(Context context)
Simple constructor to use when creating a view from code. //从代码中加载时会调用
View(Context context, AttributeSet attrs)
Constructor that is called when inflating a view from XML. //从XML中加载时会调用
View(Context context, AttributeSet attrs, int defStyleAttr)
Perform inflation from XML and apply a class-specific base style. //inflation 加载时会调用
所以要根据自己的需求实现父类的构造方法
2.重写onDraw(非必须,如果你想让界面初始化的时候显示一些东西,可以在此方法内绘制)
简单示例:随手指移动的小球
public class DrawCircel extends View {private Paint paint = new Paint();private float x = 10;private float y = 10;// 保证此view可以从代码中加载public DrawCircel(Context context) {super(context);}// 保证此view可以从XML中加载public DrawCircel(Context context, AttributeSet set) {super(context, set);}// 绘制内容时调用@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setColor(Color.RED);canvas.drawCircle(x, y, 10, paint);}// 位该View的触碰编写事件@Overridepublic boolean onTouchEvent(MotionEvent event) {x = event.getX();y = event.getY();invalidate();// 通知View重绘,会调用onDraw方法return true;}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.example.movecircle.DrawCircel android:layout_width="wrap_content" android:layout_height="wrap_content" > </com.example.movecircle.DrawCircel></RelativeLayout>
1 0
- 如何自定义自己的VIEW
- 自定义View如何定义和使用自己的属性
- iOS 自定义view 如何不使用self.view也就是系统自带的 view,使用自己定制的view
- 自定义View自己的简单理解
- 自己学习自定义view的一些总结
- 自定义View,绘制自己的手表Demo
- 自定义view学习-创建自己的九宫格解锁view
- 自定义View如何在自己类内部进行更新
- 自定义View<2> 完全自定义控件,自己画出自己的外观,自己实现事件
- 请教如何自定义一个这样的View
- 请教如何自定义一个这样的View
- 如何自定义内部有多个View的ProgressBar?
- 在view中如何保存自定义view的状态
- 如何测量自定义View时View的大小
- 自定义view (二) view自己绘制
- 自定义布局执行流程之 画出自己定义的View
- Android自定义View时添加自己的监听器
- Android自定义View时添加自己的监听器
- 第四章:位置感知搜索
- Struts2系统学习(6)action中方法调用的三种方式总结
- 关于<dl>的居中
- future模式
- 程序在eclipse运行正常,但是打包上传到服务器上却报“java.lang.reflection.invocationTargetException”
- 如何自定义自己的VIEW
- netty5 学习笔记--InternalThreadLocalMap
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
- QXmlStreamWriter 写入xml 文件
- Spark编程指南
- error while loading shared libraries xxx.so.x 错误的原因和解决办法
- android ViewPager
- jna对结构体、指针、引用、拷贝参数传递的使用
- Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置