java - Android 编辑文本被截断

标签 java android android-edittext ondraw

我在 onDraw 中创建了一个自定义 EditText View ,该 View 具有红色边距线。问题是当你向下滚动时,该线消失了。此代码只是绘制一个边距,其宽度是在值文件夹中预定义的,然后使用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);
}

     }

最佳答案

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

关于java - Android 编辑文本被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302997/

相关文章:

java - 不指定数据库的JDBC MYSQL连接?

java - 如何提示用户输入 0 到 100 之间的数字?

java - 一个 WebView 中的多个 URL

android - 对话框android中的EditText

android - 为 EditText 的 HINT 设置多行

java - 我的 Java 程序出现 FileSystemException 错误

不使用 setString 的 Java SQL 转义

android - AsyncTask 在 android 4.0 中无法正常工作

android - android 转换到打瞌睡模式需要多长时间?

android - 用于编辑文本的电子邮件键盘