我想在屏幕上随机位置填充 100 个不同的字母。在 iPhone 上,我刚刚创建了一堆 UILabels,设置了它们的 x 和 y 位置,然后使用动画来移动它们。
在 Android 上,我似乎无法将 TextView 添加到我的视图并指定其 X 和 Y。有没有办法做到这一点?
View gameView = findViewById(R.id.gameboard);
tv = new TextView(gameView.getContext());
tv.setText("A");
tv.setWidth(w); tv.setHeight(h);
// How to set the X and Y?
编辑:解决方案是使用 AbsoluteLayout:
AbsoluteLayout al = (AbsoluteLayout)findViewById(R.id.gb_layout);
tv = new TextView(this);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
params.x = 50;
params.y = 50;
al.addView(tv, params);
并根据 MotionEvent me 移动它:
AbsoluteLayout.LayoutParams p = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,(int)me.getX(), (int)me.getY());
mTV.setLayoutParams (p);
我认为你正在制作一个游戏,为此你应该研究 SurfaceViewhere就是一个很好的例子
1 ...如果你查看代码,有 onDraw 方法,你将获得屏幕的宽度和高度
2...现在将此宽度和高度作为随机生成器的种子,获取 x 和 y 位置。
3...迭代第2点100次,你就会得到想要的结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)