如何在textview中创建行?

时间:2011-10-14 07:26:11

标签: android

我正在创建一个看起来类似于笔记本页面的textview。那我怎样才能在textview中创建行?有人可以帮忙??

1 个答案:

答案 0 :(得分:1)

您可以使用记事本示例,但请特别注意NoteEditor.java中的LineEditText类

public static class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
}