当前位置: 移动技术网 > IT编程>移动开发>Android > Android 自定义View的使用介绍

Android 自定义View的使用介绍

2019年07月24日  | 移动技术网IT编程  | 我要评论

yy频道设计样本,花生的功效与作用,孔明智退司马懿

在项目开发中,可能系统自带的一些widget不能满足我们的需求,这时就需要自定义view。

通过查看系统中的常用widget如button,textview,edittext,他们都继承自view,所以我们在继承自定义view的时候也自然的需要继承view。
1、首先新建一个类lview继承自view

复制代码 代码如下:

public class lview extends view {
 private paint paint;

 public lview(context context) {
  super(context);
 }

 public lview(context context, attributeset attrs) {
  super(context, attrs);
 }

 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  paint = new paint();// new一个画笔
  paint.setcolor(color.red);// 设置画笔颜色
  paint.setstyle(style.fill);// 设置画笔填充
  canvas.drawcircle(50, 50, 40, paint);// 用画笔在画布上添加一个圆,不只可以添加圆,还可以添加矩形等!
  paint.setcolor(color.yellow);// 设置画笔颜色
  canvas.drawtext("lview", 50, 50, paint);// 用画笔在画布上添加文字,中间两个参数对应的是坐标。
 }
}


2、在layout文件中进行配置
复制代码 代码如下:

 <button
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/hello_world" />

<com.androidstudy.lview
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />


运行程序,可以看到如下画面:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网