您好,登录后才能下订单哦!
本文地址: 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
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="notepad_paper">#EEF8E0A0</color> <color name="notepad_lines">#FF0000FF</color> <color name="notepad_margin">#90FF0000</color> <color name="notepad_text">#AA0000FF</color> </resources>
位置: res->values->dimen.xml
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="notepad_margin">30dp</dimen> </resources>
位置: res->layout->todolist_item.xml
<?xml version="1.0" encoding="utf-8"?> <mzx.spike.todolist.app.ToDoListItemView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:scrollbars="vertical" android:textColor="@color/notepad_text" android:fadingEdge="vertical" />
1. 标签为类名, 即ToDoListItemView类, 重载了TextView的方法;
2. 设置相应的属性标签;
位置: java->package->ToDoListActivity
...... int resID = R.layout.todolist_item; //三个参数 aa = new ArrayAdapter<String>(this, resID, toDoItems); toDoListFragment.setListAdapter(aa); ......
找到资源文件的ID, 传入适配器;
代码下载: http://download.csdn.net/detail/u012515223/7050371
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。