简体   繁体   English

Android:Textview中的格线/水平线

[英]Android: Ruled/horizontal lines in Textview

In Android, I have a TextView and its content will be dynamic. 在Android中,我有一个TextView,它的内容是动态的。 I want to show a horizontal line after each line of text. 我想在每行文本之后显示一条水平线。 I have searched a lot and found it for EditText( How to use ruled/horizontal lines to align text in EditText in Android? ). 我已经搜索了很多东西,并找到了EditText( 如何在Android中的EditText中使用格线/水平线对齐文本? )。 I was planing to draw dynamic TextView and a horizontal line under them. 我打算在其下绘制动态TextView和一条水平线。 But I don't know how can I detect an end of line. 但是我不知道如何检测行尾。 Any help will be highly appreciated. 任何帮助将不胜感激。 I want to have the same effect as the attached image of How to use ruled/horizontal lines to align text in EditText in Android? 我希望具有与如何在Android中的EditText中使用格线/水平线对齐文本的图像相同的效果

I'm using the technique of drawing lines between each line of text in EditText and then I will make the EditText non-editable by setting setKeyListener(null) to the custom EditText object so that, the EditText acts like a TextView :) 我正在使用在EditText中的每一行文本之间绘制线条的技术,然后通过将setKeyListener(null)设置为自定义EditText对象来使EditText不可编辑,从而使EditText就像TextView一样 :)


A custom EditText that draws lines between each line of text that is displayed: 一个自定义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);
    }
} 

Now use object of LinedEditText class where you need your TextView and make it non-editable. 现在在需要TextView并使它不可编辑的地方使用LinedEditText类的对象。

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);
        et.setKeyListener(null);

        ll.addView(et);
        this.setContentView(ll);

    }

}

et.setKeyListener(null) makes the EditText non-editable so, it acts like a TextView. et.setKeyListener(null)使EditText不可编辑,因此它就像TextView。


The Output: 输出:

在此处输入图片说明

Issue with cursor: 游标问题:

If you use et.setKeyListener(null) only then it is just not listening to keys but user can see a cursor on the EditText. 如果仅使用et.setKeyListener(null),则它只是不侦听键,而是用户可以在EditText上看到光标。 If you don't want this cursor just disable the EditText by adding this line: 如果您不希望该光标通过添加以下行来禁用EditText:

 et.setEnabled(false);

You can use a View for drawing line: For horizontal line: 您可以使用视图来绘制线条:对于水平线:

<View
android:layout_height="1dp"
android:layout_width="fill_parent"
android:background="#ffffff" />
<TextView  
android:layout_height="1dp"  
android:layout_width="fill_parent"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM