Lined EditText 底线消失
Posted
技术标签:
【中文标题】Lined EditText 底线消失【英文标题】:Lined EditText bottom lines disappears 【发布时间】:2021-03-03 13:28:18 【问题描述】: 我正在使用Lined EditText
在像记事本这样的安卓应用程序中显示垂直线。当我添加数据时,它会成功显示,但随着数据的增长,底线会消失。
我们将不胜感激。
代码:
public class LinedEditText extends AppCompatEditText
private Rect mRect;
private Paint mPaint;
public LinedEditText(Context context, AttributeSet attrs)
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0xFF000000);
/**
* This is called to draw the LinedEditText object
*
* @param canvas The canvas on which the background is drawn.
*/
@Override
protected void onDraw(Canvas canvas)
int height = canvas.getHeight();
int curHeight = 0;
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (curHeight = baseline + 1; curHeight < height;
curHeight += getLineHeight())
canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
super.onDraw(canvas);
layout_file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<com.g26app.LinedEditText
android:id="@+id/note"
android:layout_
android:layout_
android:background="@android:color/transparent"
android:gravity="top"
android:textSize="22sp" />
</LinearLayout>
【问题讨论】:
【参考方案1】:这是你需要的基于max4ever的代码
@Override
protected void onDraw(Canvas canvas)
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();//for long text with scrolling
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);//first line
for (int i = 0; i < count; i++)
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
super.onDraw(canvas);
【讨论】:
【参考方案2】:我更新了LinedEditText
的onDraw,它工作正常:
public void onDraw(Canvas canvas)
int height = getHeight() / getLineHeight();
if (getLineCount() > height)
height = getLineCount();
Rect rect = this.mRect;
Paint paint = this.mPaint;
int lineBounds = getLineBounds(0, rect);
for (int i = 0; i < height; i++)
float f = (float) (lineBounds + 1);
canvas.drawLine((float) rect.left, f, (float) rect.right, f, paint);
lineBounds += getLineHeight();
super.onDraw(canvas);
【讨论】:
以上是关于Lined EditText 底线消失的主要内容,如果未能解决你的问题,请参考以下文章
使用 appcompat v7 更改 EditText 底线颜色
如何在 android 中删除 EditText 材料设计底线?