如何使用规则/水平线来对齐Android中的EditText中的文本?

基本上我想在Android中做这样的事情:

在此处输入图像描述

我试图在自定义EditText中绘制水平线,然后在这些行上键入。

我使用文本大小来表示两条水平线之间距离 。 但是, 光标大小和文本大小不同 。 因此,我无法维持在这些行上“放置”文本。

文本库与这些水平线的对齐并不合适。

以下是用于绘制线条的代码: –

float textSize = getTextSize()); Paint paint = new Paint(); for (int i = 0; i < 50; i++) { canvas.drawLine(0, textSize * i, getWidth(), textSize * i, paint); } 

EditText没有privides有任何获取游标大小的方法。

请建议是否有任何解决方法,或任何其他更好的方法来做到这一点。

一个自定义的EditText,用于在显示的每行文本之间绘制线条:

 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.STROKE); mPaint.setColor(0x800000FF); } @Override protected void onDraw(Canvas canvas) { int count = getLineCount(); Rect r = mRect; Paint paint = mPaint; for (int i = 0; i < count; i++) { int baseline = getLineBounds(i, r); canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); } super.onDraw(canvas); } } 

现在使用您需要的LinedEditText类的对象。

一个例子:

 public class HorizontalLine extends Activity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("Android: Ruled/horizonal lines in Textview"); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LinedEditText et = new LinedEditText(this, null); et.setText("The name of our country is Bangladesh. I am proud of my country :)"); et.setLayoutParams(textViewLayoutParams); ll.addView(et); this.setContentView(ll); } } 

输出:

在此处输入图像描述