Android edittext被切断了

时间:2013-01-13 11:29:31

标签: java android android-edittext ondraw

我在onDraw中创建了一个自定义的EditText视图,该视图具有红色边距线。问题是当你向下滚动时,线条会消失。此代码只是绘制一个边距,其宽度在values文件夹中预先定义,然后使用colors.xml中的预定义颜色对其进行着色。这是我的代码:

package com.lioncode.notepad;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

Public class LinedEditText extends EditText {

private static Paint linePaint;

static {
    linePaint = new Paint();
    linePaint.setColor(Color.DKGRAY);
    linePaint.setStyle(Style.STROKE);
}

public LinedEditText(Context context, AttributeSet attributes) {
    super(context, attributes);
    init();
}

private Paint marginPaint;
private float margin;
private int paperColor;

private void init() {
    Resources myResources = getResources();

    marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    marginPaint.setColor(myResources.getColor(R.color.notepad_margin));

    paperColor = myResources.getColor(R.color.notepad_paper);
    margin = myResources.getDimension(R.dimen.notepaper_margin);

}

@Override
protected void onDraw(Canvas canvas) {
    Rect bounds = new Rect();
    int firstLineY = getLineBounds(0, bounds);
    int lineHeight = getLineHeight();
    int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);

    for (int i = 0; i < totalLines; i++) {
        int lineY = firstLineY + i * lineHeight;
        canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
    }

    // draw margin
    canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);

    // move the text from along the margin
    canvas.save();
    canvas.translate(margin, 0);
    super.onDraw(canvas);
}

     }

1 个答案:

答案 0 :(得分:1)

将总行替换为用户可能永远不会达到的高数字。