从教程中我创建了布局:
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(0x80000000);
}
@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);
}
}
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.bbbfr.mynotepad.NotepadText$LinedEditText"
android:id="@+id/note"
android:background="#ffd6e5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:textColor="#000000"
android:inputType="textMultiLine"
android:capitalize="sentences"
/>
这只会使第一行加下划线。即使编辑文本中只有一行,是否可以将所有行都加下划线?
我尝试更改循环,例如 for (int i = 0; i < 5; i++)
但随后收到此错误:
04-28 08:29:05.093: E/AndroidRuntime(14398): java.lang.IndexOutOfBoundsException: 2, 1 04-28 08:29:05.093: E/AndroidRuntime(14398): at android.text.PackedIntVector.getValue( PackedIntVector.java:70) 04-28 08:29:05.093: E/AndroidRuntime(14398): 在 android.text.DynamicLayout.getLineTop(DynamicLayout.java:367) 04-28 08:29:05.093: E/AndroidRuntime( 14398): 在 android.text.Layout.getLineBottom(Layout.java:831) 04-28 08:29:05.093: E/AndroidRuntime(14398): 在 android.text.Layout.getLineBounds(Layout.java:437) 04 -28 08:29:05.093: E/AndroidRuntime(14398): 在 android.widget.TextView.getLineBounds(TextView.java:4122) 04-28 08:29:05.093: E/AndroidRuntime(14398): 在 com.bbbfr .mynotepad.NotepadText$LinedEditText.onDraw(NotepadText.java:56)
到这一行:int baseline = getLineBounds(i, r);
我也在android:lines="5"
视图中设置了。