Android - 编辑文本 - 下划线

标签 android line android-edittext underline

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

最佳答案

这种事情是在 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" />

关于Android - 编辑文本 - 下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4114859/

相关文章:

android - 如何应用汉宁窗口功能

android - 如何将 2 个外部麦克风连接到 Android 设备

android - 如何从Google Play应用中了解此类堆栈跟踪?

r - 使用 ggplot2 如何在绘制线条后用 aes() 绘制点?

android - 更改背景颜色时 EditText 边距丢失

java - 在 Edittext 上使用 setText 的 RxTextView.textChanges

android - 如何在焦点/按下时更改 ImageButton 的色调

iphone - 在 iPhone 4 上的 CATiledLayer 中绘制时出现模糊像素跨接

Java:从套接字获取多行

java - 如何更新方法以在按下按钮后从文本观察器返回更新的信息