如何在Android中的EditText中添加页面线?

Pie*_*888 3 android underline android-layout android-edittext

可以在一个EditText?中显示页面线?

我的意思是这些线:

在此输入图像描述

假设我的EditText尺寸是500 x 500像素.我希望这些线在500×500平方米的范围内可见.

有没有建立方式来做到这一点?我已经尝试了谷歌,但我找不到答案.我想我的另一个选择是动态创建一个基于textheight和linespacing的图形,这种丑陋的解决方法.

gid*_*eon 7

来自android dev网站的记事本应用程序示例向您展示了如何执行此操作.

http://developer.android.com/resources/samples/NotePad/index.html

看起来像这样(向下滚动代码):

记事本

大多数相关代码都在此文件中.注意LinedEditText内心阶级.它在活动中定义.它绘制了所需的线条.

在activity onCreate()方法里面,setContentView(R.id.note_editor)设置为视图,它在这里定义

这里提取的片段.更新:@ Pieter888修改的代码在整个EditText控件上绘制线条.

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

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

    public LinedEditText(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0xFF000000);
    }

    /**
     * This is called to draw the LinedEditText object
     * @param canvas The canvas on which the background is drawn.
     */
    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = canvas.getHeight();
        int curHeight = 0;
        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);
        for (curHeight = baseline + 1; curHeight < height; 
                                                 curHeight += getLineHeight())
        {
            canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
        }
        super.onDraw(canvas);
    }
}
Run Code Online (Sandbox Code Playgroud)