十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
興業(yè)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書(shū)合作)期待與您的合作!
本文地址: http://blog.csdn.net/caroline_wendy/article/details/21330733
Android允許從已有的視圖工具箱(Widget Tool Box)派生子類 或 實(shí)現(xiàn)自己的視圖控件;
通過(guò)重寫(xiě)事件處理程序和onDraw()方法, 但是仍然回調(diào)超類(super)的方法, 可以對(duì)視圖進(jìn)行定制, 而不必實(shí)心它的功能;
前置步驟參見(jiàn): 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() { //獲得對(duì)資源列表的引用 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. 重載構(gòu)造函數(shù), 包含三個(gè)參數(shù)的重載版本,回調(diào)超類(super)之后,初始化資源私有變量(init);
3. 在Init()中, 獲得資源列表的引用(getResource), 將資源文件轉(zhuǎn)換為可以調(diào)用的參數(shù)(myResource.getXXX),初始化資源私有變量;
4. 重寫(xiě)(Override)OnDraw方法, 設(shè)置顏色, 畫(huà)線, 指定寫(xiě)入格式;
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. 標(biāo)簽為類名, 即ToDoListItemView類, 重載了TextView的方法;
2. 設(shè)置相應(yīng)的屬性標(biāo)簽;
位置: java->package->ToDoListActivity
...... int resID = R.layout.todolist_item; //三個(gè)參數(shù) aa = new ArrayAdapter(this, resID, toDoItems); toDoListFragment.setListAdapter(aa); ......
找到資源文件的ID, 傳入適配器;
代碼下載: http://download.csdn.net/detail/u012515223/7050371