Android - ToDoList(定制样式) 详解

ToDoList(定制样式) 详解


本文地址: http://blog.csdn.net/caroline_wendy


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() {

        //获得对资源列表的引用
        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. 执行程序:

Android - ToDoList(定制样式) 详解


Android - ToDoList(定制样式) 详解



Android - ToDoList(定制样式) 详解,布布扣,bubuko.com

Android - ToDoList(定制样式) 详解

上一篇:安卓AsyncTask 使用方法


下一篇:Android利用反射修改ListView的ScollBar图标