行edittext从顶部有空格

时间:2016-08-29 04:53:59

标签: android

我想创建一个行edittext并设置它的高度匹配父级,但是当它显示有一个顶部空格时,如下图所示。

这是我的代码:

public class LineEditText extends EditText {
private Rect mRect;
private Paint mPaint;
public LineEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    mRect = new Rect();
    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setColor(Color.parseColor("#afaaaa")); 
}

@Override
protected void onDraw(Canvas canvas) {
    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();
    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();
    }

    super.onDraw(canvas);
}

edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <app.lichconggiao.custom.LineEditText
        android:id="@+id/edtNote"
        android:textSize="20dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:textColor="#212121" />
</LinearLayout>

和我的屏幕显示

enter image description here

我如何解决它? 请帮我谢谢

0 个答案:

没有答案