Android - 编辑文本 - 下划线

2023-02-25移动开发问题
16

本文介绍了Android - 编辑文本 - 下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

推荐答案

这种事情在记事本 示例演示.如果我们查看 编辑器源代码,我们可以看到他们使用自定义文本编辑器,如下所示:

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

然后他们在 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 - 编辑文本 - 下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ccDrawLine 不透明度?
ccDrawLine opacity?(ccDrawLine 不透明度?)...
2024-08-12 移动开发问题
4

两条线之间的角度是错误的
Angle between two lines is wrong(两条线之间的角度是错误的)...
2024-08-12 移动开发问题
1

如何用 Cocos2d-iPhone 画线
How to draw a line with Cocos2d-iPhone(如何用 Cocos2d-iPhone 画线)...
2024-08-12 移动开发问题
3

Cocos2d中Sprites在两点之间画一条线Sprite
Draw a Line Sprite Between Two Points made by Sprites in Cocos2d(Cocos2d中Sprites在两点之间画一条线Sprite)...
2024-08-12 移动开发问题
9

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

使用openGL和cocos2D绘制大量线条的最佳方法是什么?
What is the best way to draw a large number of lines using openGL and cocos2D?(使用openGL和cocos2D绘制大量线条的最佳方法是什么?)...
2024-08-12 移动开发问题
5