Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android layout 如何在记事本中绘制多行编辑文本?_Android Layout_Android Edittext - Fatal编程技术网

Android layout 如何在记事本中绘制多行编辑文本?

Android layout 如何在记事本中绘制多行编辑文本?,android-layout,android-edittext,Android Layout,Android Edittext,我正在创建一个记事本,我想在上面有多条水平线的编辑文本。正如我已经做了一些,但它是显示行时,我点击下一步或进入下一行。我希望电话线已经在那里了。当我在android上使用layour时:lines=5。它正在显示我所附的图片 请提出同样的建议。谢谢 编辑:我的编辑文本显示如下!!!!顶部有一个巨大的缺口。请建议怎么做?这是代码,根据谷歌的搜索结果,输出将显示在图像中。按enter键时,将添加新行 public class LinedEditText extends EditText {

我正在创建一个记事本,我想在上面有多条水平线的编辑文本。正如我已经做了一些,但它是显示行时,我点击下一步或进入下一行。我希望电话线已经在那里了。当我在android上使用layour时:lines=5。它正在显示我所附的图片

请提出同样的建议。谢谢

编辑:我的编辑文本显示如下!!!!顶部有一个巨大的缺口。请建议怎么做?

这是代码,根据谷歌的搜索结果,输出将显示在图像中。按enter键时,将添加新行

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);
    }
}
有关更多信息,请参阅链接。

这是基于google的代码,输出将显示在图像中。按enter键时,将添加新行

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);
    }
}

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

Simple在XML中添加此行
android:gravity=top | left

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

在这里,我的代码默认会画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);
    }
}

在这里,默认情况下我的代码将绘制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);
    }
}

没错,但问题是我想在编辑文本中显示10条水平线。当我在按enter键后才写一些东西时,我会得到新行,但我希望这些行已经显示在编辑文本上。嗨,你能告诉我怎么做吗?@Shweta检查我提供的参考链接。它有jkhouw1的解释,说明了如何实现这一点:当我按enter键时,它不会显示行。我怎样才能增加线。或者我需要更改getLinecount;方法?不明白你的意思。我想在布局上看到一些线条。它正在工作,但问题在于它显示的是空的空间。有空间。没错,但问题是我想在编辑文本中显示10条水平线。当我在按enter键后才写一些东西时,我会得到新行,但我希望这些行已经显示在编辑文本上。嗨,你能告诉我怎么做吗?@Shweta检查我提供的参考链接。它有jkhouw1的解释,说明了如何实现这一点:当我按enter键时,它不会显示行。我怎样才能增加线。或者我需要更改getLinecount;方法?不明白你的意思。我想在布局上看到一些线条。它正在工作,但问题在于它显示的是空的空间。有空间的。