android - 如何将页面行添加到 Android 中的 EditText?

标签 android android-layout android-edittext underline

是否可以在 EditText 中显示分页线?

我的意思是这些行:

enter image description here

假设我的 EditText 大小为 500 x 500 像素。我希望这些线条在 500 x 500 的正方形上可见。

是否有内置方法可以做到这一点?我已经尝试过谷歌,但找不到答案。 我想我的另一个选择是根据文本高度和行距动态创建图形,这是一个丑陋的解决方法。

最佳答案

来自 android 开发站点的记事本应用程序示例向您展示了如何执行此操作。

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

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

Notepad

大部分相关代码在 this file 中.注意 LinedEditText 内部类。它在 Activity 中定义。它绘制所需的线条。

在 Activity onCreate() 方法中,setContentView(R.id.note_editor) 被设置为 View ,它被定义为here

摘自 here 的 fragment . 更新:由@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);
    }
}

关于android - 如何将页面行添加到 Android 中的 EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992411/

相关文章:

android - 使应用程序与当前 Activity 通信

android - 如何在 Android Gradle 任务中注册生成的资源?

android - SupportMapFragment 中如何使用深色模式?

android - 是否有像素密度为 xxhdpi 的 720dp 设备?

java - 使用 MaskedEditText 时出错

android - Android 中的 EditText(货币)验证

android - 如何删除未使用的 GcmTaskService 服务?

android - 隐藏 AppBarLayout 并将其空间留给剩余的 View

android-layout - 如何防止键盘将操作栏推离屏幕

java - 自定义键盘 : handling inputType change