Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> EditView變形 使得每行都有一個下劃線

EditView變形 使得每行都有一個下劃線

編輯:關於android開發

  代碼如下:

  /**
        * A custom EditText that draws lines between each line of text that is displayed.
        * 這是一個自定義的EditView被畫很多行再每兩行的中間
        */
       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(0x8FF00FF);
           }
           /**
            * 這個是具體的畫法
            *
            * (non-Javadoc)
            * @see android.widget.TextView#onDraw(android.graphics.Canvas)
            */
           @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);
           }
       }

  關鍵就是找到他的基准線然後再canvas上面畫出來就行

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved