我的自定义视图中有扩展EditText的下一个代码:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int count = getLineCount();
Canvas cvs= new Canvas();
Drawable dr = this.getBackground();
Rect r = mRect;
Paint paint = mPaint;
mTextPaint.setTextSize(this.getTextSize());
Paint PaintText = mTextPaint;
for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r);
cvs.drawText(Integer.toString(i+1), r.left, baseline + 1, PaintText);
cvs.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
}
cvs.drawLine(PaintText.measureText(Integer.toString(count)), this.getTop(), PaintText.measureText(Integer.toString(count)), this.getBottom(), paint);
dr.setBounds(0, 0, this.getRight(), this.getBottom());
dr.draw(cvs);
this.setBackgroundDrawable(dr);
}
为什么这景色后面没有什么东西?
最佳答案
protected void onDraw(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
int count = getLineCount();
Canvas cvs= new Canvas(bitmap);
cvs.drawColor(Color.rgb(245, 245, 245));
Rect r = mRect;
Paint paint = mPaint;
mTextPaint.setTextSize(this.getTextSize());
Paint PaintText = mTextPaint;
for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r);
cvs.drawText(Integer.toString(i+1), 2, baseline + 1, PaintText);
cvs.drawLine(2, baseline + 1, r.right, baseline + 1, paint);
}
cvs.drawLine(PaintText.measureText(Integer.toString(count))+3, this.getTop(), PaintText.measureText(Integer.toString(count))+3, this.getBottom(), paint);
this.setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), bitmap));
this.setPadding((int) (PaintText.measureText(Integer.toString(count))+7),3,3,3);
super.onDraw(canvas);
}
关于android - 如何在View的背景上设置drawable? Android的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5444711/