Android中的ToDoList(定製樣式) 詳解

來源:互聯網
上載者:User

Android允許從已有的視圖工具箱(Widget Tool Box)派生子類 或 實現自己的視圖控制項;

通過重寫事件處理常式 和onDraw()方法, 但是仍然回調超類(super)的方法, 可以對視圖進行定製, 而不必實心它的功能;

前置步驟參見: http://blog.csdn.net/caroline_wendy/article/details/21246963

步驟:

1. 建立ToDoListItemView類, 定製Item項的外觀:

位置: 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() {                //獲得對資源清單的引用 // 查看本欄目更多精彩內容:http://www.bianceng.cn/OS/extra/         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距離;

2. 建立顏色(colors)資源檔

位置: 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>

顏色資源檔, 以color標籤, Android Studio會顯示顏色;

3. 修改尺寸(dimen)資源檔:

位置: 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>

補充即可;

4. 建立todolist_item布局檔案:

位置: 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. 設定相應的屬性標籤;

5. 修改適配器(Adapter), 使用定製的TextView:

位置: java->package->ToDoListActivity

......          int resID = R.layout.todolist_item;          //三個參數          aa = new ArrayAdapter<String>(this, resID, toDoItems);                toDoListFragment.setListAdapter(aa);  ......

詳解:

找到資源檔的ID, 傳入適配器;

6. 執行程式:代碼下載: http://download.csdn.net/detail/u012515223/7050371

作者:csdn部落格 Spike_King

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.