【发布时间】:2012-02-14 23:00:42
【问题描述】:
我有一个小问题...
是否可以在SurfaceView 中实现ImageView,
或在SurfaceView 内创建ImageView
如果是这样,有人可以为我指出正确的方向。
【问题讨论】:
标签: java android imageview surfaceview implements
我有一个小问题...
是否可以在SurfaceView 中实现ImageView,
或在SurfaceView 内创建ImageView
如果是这样,有人可以为我指出正确的方向。
【问题讨论】:
标签: java android imageview surfaceview implements
不,您不能将子 Views 添加到 SurfaceView。
您可以将ImageView 和SurfaceView 都放在其他容器中,并且某些容器(RelativeLayout、FrameLayout)允许后面的子元素浮在前面的子元素之上(Z 轴排序)。因此,您可以通过这种方式在SurfaceView 中赋予ImageView 的视觉外观。
【讨论】:
SurfaceView 放入XML 中的RelativeLayout。然后,在运行时,只需在RelativeLayout 上调用addView() 以添加您的ImageView,使用适当的RelativeLayout.LayoutParams。
我在代码中使用 TextView 进行了此操作,但您可以使用 ImageView 更改文本视图。
我的 TextView 是顶部的一个小条,因此参数不断变化。
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
ourSurfaceView = new SurfGame(this);
testbox = new LinearLayout.LayoutParams(widthx,heighty/30);
layout.addView(tv,testbox);
layout.addView(ourSurfaceView,params);
setContentView(layout);
【讨论】: