转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47082241

在这一篇博文中,将向大家介绍如何以最简单的方式,来自定义Android中的控件,下面我们以自定义TextView为例来向大家介绍如何自定义Android中的控件。

首先,我们来简单说一下Android中自定义控件的原理:创建一个类,继承要自定义的控件类,重写父类的相关方法即可。原理说完了,是不是很简单呢?下面,我们就一起来自定义一个TextView控件吧。

1、创建工程CustomerTextView

如下图所示:

2、创建ToListItemView类

这个类扩展了TextView类,它包含一个重写的onDraw()方法,以及调用了新的init()方法的构造方法。

具体代码结构如下:

package com.lyz.customer.textview.activity;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.TextView;/** * 自定义TextView类 * 继承TextView类重写TextView的一些方法 * @author liuyazhuang * */public class ToListItemView extends TextView {/** * 构造方法 * @param context * @param attrs * @param defStyle */public ToListItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}/** * 构造方法 * @param context * @param attrs */public ToListItemView(Context context, AttributeSet attrs){super(context, attrs);init();}/** * 构造方法 * @param context */public ToListItemView(Context context){super(context);init();}/** * 初始化方法 */private void init(){}//重新绘制样式@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stub}}

3、在res/values目录下新建colors.xml文件

在这个文件中,为页面,边缘,行和文本设置新的颜色值

具体实现如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="notepad_paper">#EEF8E0A0</color>    <color name="notepad_lines">#EE0000FF</color>    <color name="notepad_margin">#EE0000FF</color>    <color name="notepad_text">#AA0000FF</color></resources>

4、创建dimens.xml文件

为页面边缘的宽度添加新值。

具体实现如下:

<resources>    <!-- Default screen margins, per the Android Design guidelines. --><dimen name="notepad_margin">30dp</dimen></resources>

5、定制ToListItemView外观

创建新的私有实例变量来存储用来绘制页面的背景和边缘的Paint对象。此外,还要分别创建用来存储页面的颜色值和边缘宽度值的变量。

通过完善init()方法,来引用在前两步中创建的实例资源,并创建Paint对象

具体实现代码如下:

//绘制页面的背景private Paint marginPaint;//绘制页面的边缘private Paint linePaint;//存储页面的颜色值private int paperColor;//存储页面的边缘宽度值private float margin;/** * 初始化方法 */private void init(){//获得最资源表的引用Resources resources = getResources();//创建在onDraw方法中使用的画刷marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);marginPaint.setColor(resources.getColor(R.color.notepad_margin));linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);linePaint.setColor(resources.getColor(R.color.notepad_lines));//获得页面背景颜色和边缘宽度paperColor = resources.getColor(R.color.notepad_paper);margin = resources.getDimension(R.dimen.notepad_margin);}
要开始绘制页面,就需要重写onDraw()方法。并使用前面创建的Paint对象来绘制图像,一旦绘制了页面图像之后,就可以调用父类的onDraw()方法,让它像往常一样绘制文本。

具体实现代码如下:

//重新绘制样式@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stub//绘制页面的颜色canvas.drawColor(paperColor);//绘制边缘canvas.drawLine(margin, 0, margin, getMeasuredHeight(), linePaint);canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);//绘制margincanvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);//移动文本,让它跨过边缘canvas.save();canvas.translate(margin, 0);//使用TextView渲染文本super.onDraw(canvas);canvas.restore();}
具体完整代码如下:

package com.lyz.customer.textview.activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.TextView;/** * 自定义TextView类 * 继承TextView类重写TextView的一些方法 * @author liuyazhuang * */public class ToListItemView extends TextView {//绘制页面的背景private Paint marginPaint;//绘制页面的边缘private Paint linePaint;//存储页面的颜色值private int paperColor;//存储页面的边缘宽度值private float margin;/** * 构造方法 * @param context * @param attrs * @param defStyle */public ToListItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}/** * 构造方法 * @param context * @param attrs */public ToListItemView(Context context, AttributeSet attrs){super(context, attrs);init();}/** * 构造方法 * @param context */public ToListItemView(Context context){super(context);init();}/** * 初始化方法 */private void init(){//获得最资源表的引用Resources resources = getResources();//创建在onDraw方法中使用的画刷marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);marginPaint.setColor(resources.getColor(R.color.notepad_margin));linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);linePaint.setColor(resources.getColor(R.color.notepad_lines));//获得页面背景颜色和边缘宽度paperColor = resources.getColor(R.color.notepad_paper);margin = resources.getDimension(R.dimen.notepad_margin);}//重新绘制样式@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stub//绘制页面的颜色canvas.drawColor(paperColor);//绘制边缘canvas.drawLine(margin, 0, margin, getMeasuredHeight(), linePaint);canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);//绘制margincanvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);//移动文本,让它跨过边缘canvas.save();canvas.translate(margin, 0);//使用TextView渲染文本super.onDraw(canvas);canvas.restore();}}

6、创建布局文件todolist_item.xml

这个文件引用的是我们自定义的控件类。

具体实现如下:

<?xml version="1.0" encoding="utf-8"?><com.lyz.customer.textview.activity.ToListItemView    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:padding="10dp"    android:scrollbars="vertical"    android:textColor="@color/notepad_text"    android:fadingEdge="vertical"    android:text="@string/hello_world"/>

7、完善MainActivity类

在MainActivity中设置我们自己定义的View

具体实现如下:

package com.lyz.customer.textview.activity;import android.app.Activity;import android.os.Bundle;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);<span style="color:#FF0000;">setContentView(R.layout.todolist_item);</span>}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

8、AndroidManifest.xml文件

最后,我们并没有在AndroidManifest.xml文件中做任何操作,AndroidManifest.xml文件中的内容都是自动生成的,下面我们还是给出AndroidManifest.xml文件中的代码吧

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.lyz.customer.textview.activity"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.lyz.customer.textview.activity.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

9、运行效果

温馨提示:大家可以到http://download.csdn.net/detail/l1028386804/8936269链接来下载完整的自定义控件示例代码

更多相关文章

  1. android中的content provider的使用
  2. Android与H5混合开发「kotlin,WebView」
  3. Android(安卓)Launcher 分析
  4. Android—TextView的XML属性和方法
  5. 深入源码解析Android中的Handler,Message,MessageQueue,Looper
  6. 详解 Android(安卓)的 Activity 组件
  7. android Adapter综合介绍
  8. android 网络连接
  9. Android来电铃声默认设置的实现方法与怎么设置语音来电的默认铃

随机推荐

  1. [Android] 使用Fragment创建动态UI
  2. 一,创建你的第一个Android项目
  3. 谷歌透漏开发Fuchsia的真正意图,取代Andro
  4. 简单新闻客户端APP设计
  5. Android核心分析 之一分析方法论探讨之设
  6. android v7兼容包RecyclerView的使用(三)—
  7. 二、Android(安卓)数据链接的动态分析
  8. Android(安卓)Touch事件传递
  9. android快速入门指南
  10. No cached version of androidx.appcompa