兴业ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
本文地址: http://blog.csdn.net/caroline_wendy/article/details/21330733
Android允许从已有的视图工具箱(Widget Tool Box)派生子类 或 实现自己的视图控件;
通过重写事件处理程序和onDraw()方法, 但是仍然回调超类(super)的方法, 可以对视图进行定制, 而不必实心它的功能;
前置步骤参见: http://blog.csdn.net/caroline_wendy/article/details/21246963
步骤:
位置: java->package->ToDoListItemView.java
package mzx.spike.todolist.app; 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; /** * Created by C.L.Wang on 14-3-16. */ public class ToDoListItemView extends TextView{ private Paint marginPaint; private Paint linePaint; private int paperColor; private float margin; public ToDoListItemView (Context context, AttributeSet ats, int ds) { super(context, ats, ds); init(); } public ToDoListItemView (Context context) { super(context); init(); } public ToDoListItemView (Context context, AttributeSet ats) { super(context, ats); init(); } private void init() { //获得对资源列表的引用 Resources myResources = getResources(); marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); marginPaint.setColor(myResources.getColor(R.color.notepad_margin)); linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); linePaint.setColor(myResources.getColor(R.color.notepad_lines)); paperColor = myResources.getColor(R.color.notepad_paper); margin = myResources.getDimension(R.dimen.notepad_margin); } @Override public void onDraw(Canvas canvas) { canvas.drawColor(paperColor); canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint); canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint); canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint); canvas.save(); canvas.translate(margin, 0); super.onDraw(canvas); canvas.restore(); } }
1. 继承TextView类, 是文本视图的定制;
2. 重载构造函数, 包含三个参数的重载版本,回调超类(super)之后,初始化资源私有变量(init);
3. 在Init()中, 获得资源列表的引用(getResource), 将资源文件转换为可以调用的参数(myResource.getXXX),初始化资源私有变量;
4. 重写(Override)OnDraw方法, 设置颜色, 画线, 指定写入格式;
5. canvas.translate(), 使输出文件, 后移margin距离;
位置: res->values->colors.xml
#EEF8E0A0 #FF0000FF #90FF0000 #AA0000FF
位置: res->values->dimen.xml
16dp 16dp 30dp
位置: res->layout->todolist_item.xml
1. 标签为类名, 即ToDoListItemView类, 重载了TextView的方法;
2. 设置相应的属性标签;
位置: java->package->ToDoListActivity
...... int resID = R.layout.todolist_item; //三个参数 aa = new ArrayAdapter(this, resID, toDoItems); toDoListFragment.setListAdapter(aa); ......
找到资源文件的ID, 传入适配器;
代码下载: http://download.csdn.net/detail/u012515223/7050371