我正在看记事本示例在Android SDK在这里看到:http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html
的是它只能绘制当前行的光标在如http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg
不过,我想显示填满屏幕,例如线http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png
任何建议将是巨大的。的code中的相应和位似乎是在这里:
保护无效的OnDraw(帆布油画){
//获取在查看文本行数。
诠释计数= getLineCount();
//获取全球矩形和画图对象
矩形R = mRect;
涂料粉刷= mPaint;
/ *
*在绘制矩形一行中的EditText每一行文字
* /
的for(int i = 0; I<计数;我++){
//获取文本的当前行的基线坐标
INT基线= getLineBounds(I,R);
/ *
*绘制一条在后台从矩形的左侧到右侧,
*在一个跌破基线垂直位置,利用画图对象
*了解详细信息。
* /
canvas.drawLine(r.left,基线+1,r.right,基线+1,油漆);
}
//调用父方法来结束
super.onDraw(画布);
}
解决方案
这是在code,根据jkhouws1's建议与谷歌的note编辑
公共类LinedEditText扩展的EditText {
私人矩形mRect;
私人油漆mPaint;
//我们需要这个构造LayoutInflater
公共LinedEditText(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
mRect =新的矩形();
mPaint =新的油漆();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(R.color.edit_note_line); //这里设置自己的颜色
}
@覆盖
保护无效的OnDraw(帆布油画){
//诠释计数= getLineCount();
INT高=的getHeight();
INT line_height = getLineHeight();
诠释计数=身高/ line_height;
如果(getLineCount()>计数)
数= getLineCount(); //长文本滚动
矩形R = mRect;
涂料粉刷= mPaint;
INT基准= getLineBounds(0,R); //第一行
的for(int i = 0; I<计数;我++){
canvas.drawLine(r.left,基线+1,r.right,基线+1,油漆);
基线+ = getLineHeight(); //下一行
}
super.onDraw(画布);
}
}
在Eclipse IDE中preSS按Ctrl + Shift + O添加所有需要进口
I was taking a look at the notepad sample in the android SDK see here: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html
Thing is it only draws the current line the cursor is on e.g http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg
But I'd like to display lines that fill up the screen e.g. http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png
Any suggestions would be great. The relevent bit of code seems to be here:
protected void onDraw(Canvas canvas) {
// Gets the number of lines of text in the View.
int count = getLineCount();
// Gets the global Rect and Paint objects
Rect r = mRect;
Paint paint = mPaint;
/*
* Draws one line in the rectangle for every line of text in the EditText
*/
for (int i = 0; i < count; i++) {
// Gets the baseline coordinates for the current line of text
int baseline = getLineBounds(i, r);
/*
* Draws a line in the background from the left of the rectangle to the right,
* at a vertical position one dip below the baseline, using the "paint" object
* for details.
*/
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
}
// Finishes up by calling the parent method
super.onDraw(canvas);
}
解决方案
This is the code, based on jkhouws1's suggestion and google's note editor
public 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.FILL_AND_STROKE);
mPaint.setColor(R.color.edit_note_line); //SET YOUR OWN COLOR HERE
}
@Override
protected void onDraw(Canvas canvas) {
//int count = getLineCount();
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();//for long text with scrolling
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);//first line
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
}
super.onDraw(canvas);
}
}
In Eclipse IDE press Ctrl+Shift+O to add all needed imports
相关推荐
最新文章