您好,登錄后才能下訂單哦!
本文地址: 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。