0

Android アプリケーションに TableLayout があります。

テーブルの要素として動的サイズの Rectangles が必要です。キャンバスを使用して長方形を描きましたが、これらの長方形をテーブルの列に配置するにはどうすればよいですか。

私はアンドロイドが初めてです、助けてください、ありがとう。

4

1 に答える 1

2

class のcanvasメソッドで使用する必要があります。これが私の例です: クラスはTextViewクラスを拡張します:onDraw(Canvas canvas)ViewCustomView

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);
}
于 2013-04-28T13:19:27.980 に答える