2011.09.26(3)——— android sample之Notepad(带下划线的TestView)
16lz
2022-03-07
2011.09.26(3)——— android sample之Notepad(带下划线的TestView)
参考:http://www.cnblogs.com/phinecos/archive/2009/08/27/1555221.html
notepad里面用于记事的TextView
主要工作就是重载onDraw方法,利用从TextView继承下来的getLineCount函数获取文本所占的行数,以及getLineBounds来获取特定行的基准高度值,而且这个函数第二个参数会返回此行的“外包装”值。再利用这些值绘制这一行的线条。
xml
参考: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"/>
更多相关文章
- android binder机制
- Android(安卓)控件之DatePicker,TimePicker,Calender
- Android打开通讯录并获取数据
- 关于在Android设置全局变量随时获取context
- android log机制——输出log
- Android获取SD卡上图片和视频文件及其缩略图
- android 5.1 屏蔽MENU键 返回键唤醒系统
- 箭头函数的基础使用
- Python技巧匿名函数、回调函数和高阶函数