并排放置矩形和文本Android

时间:2014-08-14 12:58:14

标签: java c# android canvas xamarin

我试图在Android画布中并排放置矩形和文字。我能够绘制它们但是它们在很多方面都是重叠的。矩形和文本是动态的。我目前的代码是

int left = 50;
int top = 50;
int width = 50;
int height = 50;
for (int row = 0; row < rows; row++) {
    for (int col = 0; col < cols; col++) {
        rectanglePaint.Color = (Color.ParseColor("#CD5C5C"));
        canvas.DrawRect(left, top, left + width, top + height, rectanglePaint);
        left = (left + width + 50);
        rectanglePaint.TextSize = 30;
        canvas.DrawText("Mytext", left, top + height - 10, rectanglePaint);
    }
    top = top + height + 10;
}

任何人都可以帮我解决这个问题吗? 注意:我没有足够的声誉来发布图片。

编辑:(我的新代码如下):

for (int col = 0; col < 1; col++) { // draw 4 columns
    rectanglePaint.Color = (Color.ParseColor("#CD5C5C"));
    canvas.DrawRect(left, top, left + width, top + height, rectanglePaint);
    left = (left + width + 50);
    string text = "Mytext";
    int spacing = 10;


    canvas.DrawText(text, left, top + height - 10, rectanglePaint);
 left += rectanglePaint.MeasureText(text) + spacing;
    //left = (left + width + 10); // set new left co-ordinate + 10 pixel gap
    //rectanglePaint.TextSize = 30;
    //canvas.DrawText("Mytext",left+5,top+height-10,rectanglePaint);
    // Do other things here
    // i.e. change colour
}

1 个答案:

答案 0 :(得分:0)

我认为如果你改变

,这应该会让你走上正确的道路
canvas.DrawText("Mytext",left,top+height-10,rectanglePaint);

int spacing = 10;
String text = "Mytext";
canvas.DrawText(text,left,top+height-10,rectanglePaint);
left += rectanglePaint.measureText(text) + spacing;

您可能需要更改spacing的值,以获得文本与下一个方格之间所需的间距。