Android - edittext - underline

前端 未结 1 804
醉话见心
醉话见心 2020-12-15 02:01

Is there a way to make all lines in a multi-line edittext underlined? I want all lines to show the line, even if there\'s no text on it.

相关标签:
1条回答
  • 2020-12-15 02:37

    This sort of thing is done in the Notepad sample demo. If we look at the editor source, we can see they use a custom text editor, like this :

    /**
     * A custom EditText that draws lines between each line of text that is displayed.
     */
    public static 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);
        }
    }
    

    that they then declare this way in the layout :

    <view xmlns:android="http://schemas.android.com/apk/res/android"
        class="com.example.android.notepad.NoteEditor$LinedEditText"
        android:id="@+id/note"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:padding="5dp"
        android:scrollbars="vertical"
        android:fadingEdge="vertical"
        android:gravity="top"
        android:textSize="22sp"
        android:capitalize="sentences" />
    
    0 讨论(0)
提交回复
热议问题