如何使用排除/水平线对准的EditText文本的Andr​​oid? [英] How to use ruled/horizontal lines to align text in EditText in Android?

查看:221
本文介绍了如何使用排除/水平线对准的EditText文本的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在一个自定义绘制水平线的EditText ,然后键入这些行。

我现在用的是文字的大小作为的两条水平线之间的距离的。然而,在尺寸光标的文字,这是不一样的。因此,我不能够保持开这些线放置文本。

文本基础,这些水平线的对齐方式不来是正确的。

下面是用于绘制线条code: -

 浮动TEXTSIZE = getTextSize());
涂料粉刷=新的油漆();
的for(int i = 0; I< 50;我++){
    canvas.drawLine(0,TEXTSIZE *我的getWidth(),TEXTSIZE * I,油漆);
}
 

EditText上没有privides有什么方法获取光标大小。

请说明是否有任何解决此,或这样做的任何其他更好的办法。

解决方案

自定义EditText上绘制文本显示的每一行之间的行:

 公共类LinedEditText扩展的EditText {
    私人矩形mRect;
    私人油漆mPaint;

    //我们需要这个构造LayoutInflater
    公共LinedEditText(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);

        mRect =新的矩形();
        mPaint =新的油漆();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        诠释计数= getLineCount();
        矩形R = mRect;
        涂料粉刷= mPaint;

        的for(int i = 0; I<计数;我++){
            INT基线= getLineBounds(I,R);

            canvas.drawLine(r.left,基线+1,r.right,基线+1,油漆);
        }

        super.onDraw(画布);
    }
}
 


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

一个例子:

 公共类Horizo​​ntalLine延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setTitle(机器人:在TextView中直纹/卧式线);

        的LinearLayout LL =新的LinearLayout(本);
        ll.setOrientation(LinearLayout.VERTICAL);
        的LayoutParams textViewLayoutParams =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

        LinedEditText等=新LinedEditText(这一点,NULL);
        et.setText(我们国家的名称是孟加拉国我为我的国家感到骄傲:)。);
        et.setLayoutParams(textViewLayoutParams);

        ll.addView(等);
        this.setContentView(Ⅱ);

    }

}
 


的输出:

Basically I want to do something like this in Android:

I am trying to draw horizontal lines in a custom EditText, and then typing on these lines.

I am using the text size for the distance between two horizontal lines. However, the size of cursor and that of text is not same. Hence, I am not able to maintain placing text "on" these lines.

The alignment of the text base to these horizontal lines are not coming as proper.

Here is the code used for drawing the lines:-

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

EditText doesn't privides has any method for getting the cursor size.

Kindly suggest if there is any workaround for this, or any other better way of doing this.

解决方案

A custom EditText that draws lines between each line of text that is displayed:

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


Now use object of LinedEditText class where you need.

An Example:

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

    }

}


The Output:

这篇关于如何使用排除/水平线对准的EditText文本的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆