【发布时间】:2013-01-23 20:14:26
【问题描述】:
我有一个如下所示的自定义视图:
public class CustomView extends View {
protected Context c;
protected String text;
... // and some more useful member variables...
public CustomView(String text, Context c, ...) {
this.text = text; this.c = c;
...
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
LinearLayout ll = new LinearLayout(c);
TextView tv = new TextView(c);
tv.setText(text);
ll.addView(tv);
ll.draw(canvas);
}
在我的主要活动中,我这样做:
RelativeLayout gamelayout = (RelativeLayout) findViewById(R.id.gamelayout);
CustomView customview = new CustomView("Textview text", this);
gamelayout.addView(customview);
我的问题是,根本没有绘制任何东西,绘制的 TextView 不会出现在“游戏布局”中。我做错了什么?
【问题讨论】:
-
您没有将文本视图附加到任何实际显示在屏幕上的父级!而且我不确定你想在这里做什么
-
这就是我想要通过 gamelayout.addView(customview) 做的。至少据我所知,这将调用自定义视图的 onDraw 方法。游戏布局显示在屏幕上。
-
你可以试试 this.addView(ll); ...在调用 ll.draw(canvas); 之前将 ll 附加到 gameLayout;
标签: android view android-canvas draw