自定义有横线的TextView和EditText
思路:
首先进行初始化画笔,设置其颜色等等
`private void initViews(){ this.paint = new Paint(); this.paint.setColor(Color.GRAY); this.paint.setStrokeWidth(1.0f); this.paint.setStyle(Paint.Style.FILL); setLineSpacing(4.0f,1.2f); }
`
其次是重写onDraw()方法,绘制横线
`@Override protected void onDraw(Canvas canvas){ //总高度除于每行的高度,+1 底部也要画一根线 for(int i = 1;i<(getHeight()/getLineHeight());i++){ canvas.drawLine((float)(getPaddingLeft()+2),(float)(getLineHeight()*i),(float)(getWidth()-getPaddingRight()-2);(float)(getLineHeight()*i),this.paint); } }`
- ok ,完工.
- 其实自定义的有横线的TextView也是一样的。onDraw()方法一模一样
最后附上布局
`<?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"> <custom.LineEditText android:layout_width="match_parent" android:singleLine="false" android:gravity="top|center|left" android:layout_height="match_parent" /> </LinearLayout>`