1

在此处输入图像描述我正在创建一个记事本,我想要编辑带有多条水平线的文本。正如我对一些所做的那样,但是当我单击下一步或进入下一行时它正在显示行。我希望那些线条已经存在。当我使用带有 android:lines="5" 的层时。它正在显示我附上的图片。

请提出相同的建议。谢谢在此处输入图像描述

已编辑:我的 EditText 显示如下!!!!顶部有一个巨大的差距。请建议怎么做?

4

3 回答 3

3

在此处输入图像描述
这是代码,基于谷歌的笔记编辑器输出将显示在图像中。当你按下回车键时,将添加新行。

public 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.FILL_AND_STROKE);
        mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //int count = getLineCount();

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();//for long text with scrolling

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);//first line

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        super.onDraw(canvas);
    }
}

有关更多信息,请参阅链接。

于 2013-01-16T06:47:25.573 回答
2

简单地在你的 XML 中添加这一行 android:gravity="top|left"

于 2013-10-04T14:46:04.353 回答
0

在这里,我的代码默认绘制 15 行,您可以通过按 Enter 获得更多行:-

package com.wysiwyg.main;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

public class LineEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;
    // we need this constructor for LayoutInflater
    public LineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.BLUE); //SET YOUR OWN COLOR HERE
        setMinLines(15);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;
        if(getLineCount() > count){
            count = getLineCount();
        }
        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight();//next line
        }

        // Finishes up by calling the parent method
        super.onDraw(canvas);
    }
}
于 2013-11-21T06:43:56.290 回答