2011.09.26(3)——— android sample之Notepad(带下划线的TestView)
参考:http://www.cnblogs.com/phinecos/archive/2009/08/27/1555221.html

notepad里面用于记事的TextView

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(0x800000FF);        }                @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);        }    }


主要工作就是重载onDraw方法,利用从TextView继承下来的getLineCount函数获取文本所占的行数,以及getLineBounds来获取特定行的基准高度值,而且这个函数第二个参数会返回此行的“外包装”值。再利用这些值绘制这一行的线条。

xml

<view xmlns:android="http://schemas.android.com/apk/res/android"    class="com.example.android.notepad.NoteEditor$LinedEditText"    android:id="@+id/note"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/transparent"    android:padding="5dip"    android:scrollbars="vertical"    android:fadingEdge="vertical"    android:gravity="top"    android:textSize="22sp"    android:capitalize="sentences"/>


更多相关文章

  1. android binder机制
  2. Android(安卓)控件之DatePicker,TimePicker,Calender
  3. Android打开通讯录并获取数据
  4. 关于在Android设置全局变量随时获取context
  5. android log机制——输出log
  6. Android获取SD卡上图片和视频文件及其缩略图
  7. android 5.1 屏蔽MENU键 返回键唤醒系统
  8. 箭头函数的基础使用
  9. Python技巧匿名函数、回调函数和高阶函数

随机推荐

  1. “互联网+党建”让智慧党建系统更实用!
  2. Kubernetes集群容器引擎切换
  3. 入门大数据必读
  4. 华为AppGallery常用跳转与链接详解
  5. 常见的Java锁总结:公平锁,可重入锁,独享锁,互
  6. U3D优化批处理-GPU Instancing了解一下
  7. 终于掌握如何快速学习python了
  8. Serverless 时代 DevOps 的最佳打开方式
  9. 硅谷来信丨一招阻击恶意软件***:沙箱集成M
  10. php类和trait