Android 自定义带行线的EditText

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>

直接运行之后就可以看到效果了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值