码迷,mamicode.com
首页 > 移动开发 > 详细

Android之——自定义TextView

时间:2015-07-27 13:16:11      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:android   textview   控件   canvas   自定义   

转载请注明出处: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(){
	}
	
	//重新绘制样式
	@Override
	protected 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()方法,让它像往常一样绘制文本。

具体实现代码如下:

//重新绘制样式
	@Override
	protected 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);
		//绘制margin
		canvas.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);
	}
	
	//重新绘制样式
	@Override
	protected 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);
		//绘制margin
		canvas.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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		<span style="color:#FF0000;">setContentView(R.layout.todolist_item);</span>
	}

	@Override
	public 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链接来下载完整的自定义控件示例代码

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android之——自定义TextView

标签:android   textview   控件   canvas   自定义   

原文地址:http://blog.csdn.net/l1028386804/article/details/47082241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!