1、先贴出效果图
2、自定义控件的代码
public class WriteText extends EditText {
private Context mContext;
public WriteText(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
public void onDraw(Canvas canvas) {
WindowManager wm = (WindowManager) mContext.getSystemService("window");
int windowWidth = wm.getDefaultDisplay().getWidth();
int windowHeight = wm.getDefaultDisplay().getHeight();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(0x90FFA07A);
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int scrollY = getScrollY();
int scrollX = getScrollX() + windowWidth;
int innerHeight = scrollY + getHeight() - paddingBottom;
int lineHeight = getLineHeight();
int baseLine = scrollY + (lineHeight - ((scrollY - paddingTop) % lineHeight));
int x = 8;
while (baseLine < innerHeight) {
canvas.drawLine(x, baseLine, scrollX - x, baseLine, paint);
baseLine += lineHeight;
}
super.onDraw(canvas);
}
}
3、布局中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.textline.MainActivity" >
<com.example.textline.WriteText
android:id="@+id/editorName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:singleLine="false"
android:lineSpacingExtra="6dp"
android:layout_marginRight="4dip"
android:layout_marginLeft="4dip"
android:textSize="18.0dp"
android:padding="3dip"
android:gravity="top|center_vertical"
android:paddingLeft="10dip"
android:background="@null"
android:hint="请填写日志"
android:inputType="textMultiLine"
/>
</LinearLayout>
直接运行之后就可以看到效果了