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

<textarea class="c-sharp" name="code" rows="15" cols="50">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; &gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/hello&quot; /&gt; &lt;/LinearLayout&gt; </textarea>

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

<textarea class="c-sharp" name="code" rows="15" cols="50">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;A&gt; &lt;B&gt;&lt;/B&gt; &lt;/A&gt; </textarea>

其中AextendsLinerLayout,BextendsTextView.

为了帮助大家更容易理解,我写了一个简单的Demo ,具体步骤如下:

首先新建一个Android 工程 命名为ViewDemo .

然后自定义一个View 类,命名为MyView(extends View) .代码如下:

<textarea class="java" name="code" rows="15" cols="50">package com.android.tutor; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View { private Paint mPaint; private Context mContext; private static final String mString = &quot;Welcome to Mr Wei's blog&quot;; public MyView(Context context) { super(context); } public MyView(Context context,AttributeSet attr) { super(context,attr); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint = new Paint(); //设置画笔颜色 mPaint.setColor(Color.RED); //设置填充 mPaint.setStyle(Style.FILL); //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); mPaint.setColor(Color.BLUE); //绘制文字 canvas.drawText(mString, 10, 110, mPaint); } } </textarea>

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

<textarea class="xhtml" name="code" rows="15" cols="50">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; &gt; &lt;TextView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;@string/hello&quot; /&gt; &lt;com.android.tutor.MyView android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; /&gt; &lt;/LinearLayout&gt; </textarea>

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

Android高手进阶教程(三)之----Android 中自定义View的应用_第1张图片

更多相关文章

  1. 相对布局RelativeLayout的妙用:提示标签在输入框内
  2. Android开发布局系列: LinearLayout布局实现垂直水平居中
  3. 【Android-002】【常见布局】
  4. pandaboard ES学习之旅——4 Android源代码下载与编译
  5. android布局的一些知识
  6. Android studio中相对布局组件排列方式
  7. RelativeLayout布局属性
  8. android 界面布局-各个布局的属性介绍
  9. android view的几种布局方式

随机推荐

  1. android短彩信数据库设计(三)
  2. Android(安卓)API 中文 (54) —— Filterab
  3. android 零散笔记不定期更新 v16
  4. Android(安卓)1.5 1.6 2.0 2.1 2.2 2.3 3
  5. 简单的 Android(安卓)拍照并显示以及获取
  6. Android: Android(安卓)Architecture
  7. Android(安卓)应用程序的结构
  8. 用一个二维码做下载地址,自动区分是 ios
  9. x264在android平台上的编译和使用
  10. 【移动开发】Android中各种xml汇总