在Android应用程序中绘制TableLayout内的矩形
我的android应用程序中有一个表格布局。我想要一些动态大小的矩形作为表格的元素。我使用画布绘制了矩形,现在如何将这些矩形放置在表的列中 我是android新手,请帮帮我,在Android应用程序中绘制TableLayout内的矩形,android,android-canvas,Android,Android Canvas,我的android应用程序中有一个表格布局。我想要一些动态大小的矩形作为表格的元素。我使用画布绘制了矩形,现在如何将这些矩形放置在表的列中 我是android新手,请帮帮我, 谢谢,.您需要在类视图的onDraw(canvas canvas)方法中使用canvas。 以下是我的例子: CustomViewclass扩展了TextView类: Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Paint paint
谢谢,.您需要在类
视图的onDraw(canvas canvas)
方法中使用canvas
。
以下是我的例子:
CustomView
class扩展了TextView类:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
@Override
protected void onDraw(Canvas canvas) { // draw rectangle and text
paint.setColor(Color.BLACK);
paint.setStrokeWidth(2);
canvas.drawRect(0, 0, getWidth()-2, getHeight()-2, paint);
paint.setStrokeWidth(0);
paint.setColor(Color.CYAN);
canvas.drawRect(2, 2, getWidth()-4, getHeight()-4, paint );
paint.setColor(Color.BLACK);
canvas.drawText(getText().toString(), 6, getHeight()-getPaddingBottom()-6, paint);
}
在活动中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
TableRow tr = new TableRow(this);
tr.addView(new CustomView(this));
tr.addView(new CustomView(this));
tr.addView(new CustomView(this));
table.addView(tr);
tr = new TableRow(this);
tr.addView(new CustomView(this));
tr.addView(new CustomView(this));
tr.addView(new CustomView(this));
table.addView(tr);
setContentView(table);
}