聘我网

新概念招聘3.0

android view的一些问题

vote up0vote downstar

如何获取intent参数?

还有canvas.drawText好像不支持换行?

用这个方法显示多行:

canvas.drawText("这是通过继承和扩展View类来显示的。" + System.getProperty("line.separator") + "abc", 0, 50, paint);//绘制字体到屏幕

结果总是乱码。

 

1 个答复

vote up0vote downcheck

Activity中创建View时将this作为Context参数:

setContentView(new MyContentView(this));

然后在View中可以这么获取Intent参数:

Bundle bundle = ((Activity)getContext()).getIntent().getExtras();

换行问题可以用这个自定义方法修复:

void drawString(Canvas canvas, Paint paint, String str, int x, int y) {
    String[] lines = str.split(System.getProperty("line.separator"));
    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(), bounds);
        yoff += bounds.height();
    }
}
...
drawString(canvas, paint, "这是通过继承和扩展View类来显示的。" + System.getProperty("line.separator") + "abc", 0, 50);
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.