【问题标题】:Lined EditText bottom lines disappearsLined EditText 底线消失
【发布时间】: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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.g26app.LinedEditText
        android:id="@+id/note"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:gravity="top"
        android:textSize="22sp" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-edittext android-canvas


    【解决方案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】:

      我更新了LinedEditTextonDraw,它工作正常:

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

      【讨论】:

        猜你喜欢
        • 2015-02-23
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 2019-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多