Android中View的使用深入解析

来源:爱站网时间:2018-10-09编辑:网友分享
今天小编跟大家分享一篇关于Android中View的使用深入解析,感兴趣的朋友跟小编一起来了解一下吧!

  今天小编跟大家分享一篇关于Android中View的使用深入解析,感兴趣的朋友跟小编一起来了解一下吧!

  在项目开发中,可能系统自带的一些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文件中进行配置

  复制代码 代码如下:

  

  android:id="@+id/btn"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="@string/hello_world" />

  

  android:layout_width="wrap_content"

  android:layout_height="wrap_content" />

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

  以上就是对Android中View使用的一个大致介绍,如果你想知道得更多,更多相关内容请继续关注爱站技术频道。

上一篇:Android LayoutInflater的使用详细解析

下一篇:深度解析JSON对象

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载