【问题标题】:Android - edittext - underlineAndroid - 编辑文本 - 下划线
【发布时间】:2011-05-06 02:49:49
【问题描述】:

有没有办法让多行编辑文本中的所有行都加下划线? 我希望所有行都显示该行,即使上面没有文字。

【问题讨论】:

    标签: android line android-edittext underline


    【解决方案1】:

    这种事情是在Notepad 示例演示中完成的。如果我们查看editor source,我们可以看到他们使用自定义文本编辑器,如下所示:

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

    然后他们在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" />
    

    【讨论】:

    • 我怎样才能得到写在这个视图上的文本?
    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2015-07-18
    相关资源
    最近更新 更多