首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android -如何在屏幕中适配图形标题

Android -如何在屏幕中适配图形标题
EN

Stack Overflow用户
提问于 2012-12-07 18:49:17
回答 1查看 460关注 0票数 0

我正在使用achartengine来显示折线图。我的图表标题太长了。正因为如此,一些文本超出了屏幕的范围。现在我想让它适合屏幕宽度(可以在多行中设置它吗?)。我试过了,但我就是不明白。有人能帮我吗。

参考图像

EN

回答 1

Stack Overflow用户

发布于 2012-12-07 19:47:02

首先,也是最简单的方法是在标题First line\nSecond line中添加换行符。

第二种方法是修改achart的源代码。AbstractChart类中有drawString方法。我不知道它是不是绘制图形标题,但它会让你知道它是如何做到的。

代码语言:javascript
复制
/**
 * Draw a multiple lines string.
 * 
 * @param canvas the canvas to paint to
 * @param text the text to be painted
 * @param x the x value of the area to draw to
 * @param y the y value of the area to draw to
 * @param paint the paint to be used for drawing
 */
protected void drawString(Canvas canvas, String text, float x, float y, Paint paint) {
    String[] lines = text.split("\n");
    Rect rect = new Rect();
    int yOff = 0;
    for (int i = 0; i < lines.length; ++i) {
        canvas.drawText(lines[i], x, y + yOff, paint);
        paint.getTextBounds(lines[i], 0, lines[i].length(), rect);
        yOff = yOff + rect.height() + 5; // space between lines is 5
    }
}

您必须确定需要多少行。我们可以使用paint的measureText(String)方法测量文本宽度。然后,如果文本宽度大于可用宽度,则将文本分成两行。

代码语言:javascript
复制
if (paint.measureText(text) > canvas.getWidth()) {
    ... // Split text in two lines
        // For example you can do following steps
        // 1. Find last position of space with `text.lastIndesOf(' ')`.
        // 2. Then take substring from beginning of text to found last position of space.
        // 3. Try again with `paint.measureText` if substing fits in available width.
        // 4. In case it fits - insert line break instead of space, if not start again from 1. (find location of pre-last space, get substring from start to found location, check if it fits and so on...)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13761585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档