Android之——自己定义TextView

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

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

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

1、创建projectCustomerTextView

例如以下图所看到的:

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

原文地址:https://www.cnblogs.com/claireyuancy/p/6908030.html