以编程方式将子视图添加到 FrameLayout,然后更改它们的位置
我正在做一个增强现实应用程序,我需要在相机视图上显示一些对象。我正在创建一个 FrameLayout,向其中添加相机视图,然后添加 AR 对象(现在我正在尝试使用 TextView)。在下面的代码中,我添加到 FrameLayout 的 TextView 未显示...
有人可以帮忙吗?一旦显示 fs
,我如何以编程方式更改其位置?
更新
我尝试使用 ImageView 而不是 TextView,它有效......为什么!?
public class ARActivity extends Activity{
private CBCameraView cv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cv = new CBCameraView(this);
FrameLayout rl = new FrameLayout(this);
rl.addView(cv, GlobalVars.screenWidth, GlobalVars.screenHeight);
setContentView(rl);
// this works...
/*ImageView fs = new ImageView(this);
fs.setImageResource(R.drawable.icon);
rl.addView(fs);*/
TextView fs = new TextView(this);
fs.setText("Bar seco");
fs.setTextColor(android.R.color.white);
fs.setTextSize(1,14);
fs.setLayoutParams(new ViewGroup.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rl.addView(fs);
}
}
I'm doing an Augmented Reality app and I need to show some objects on the camera view. I'm creating a FrameLayout to which I add my camera view and then I add my AR objects (now I'm trying with TextView). In the code below the TextView I add to my FrameLayout is not shown...
Anybody can help? And once I get fs
shown, how can I change its position programmatically?
UPDATE
I tried with an ImageView instead of the TextView, and it works... Why!?
public class ARActivity extends Activity{
private CBCameraView cv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cv = new CBCameraView(this);
FrameLayout rl = new FrameLayout(this);
rl.addView(cv, GlobalVars.screenWidth, GlobalVars.screenHeight);
setContentView(rl);
// this works...
/*ImageView fs = new ImageView(this);
fs.setImageResource(R.drawable.icon);
rl.addView(fs);*/
TextView fs = new TextView(this);
fs.setText("Bar seco");
fs.setTextColor(android.R.color.white);
fs.setTextSize(1,14);
fs.setLayoutParams(new ViewGroup.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rl.addView(fs);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能出在您设置文本颜色的方式上。 setTextColor() 采用颜色值参数,而不是资源 ID。 如果您想以编程方式更改子视图位置,您应该调用,
您可以通过布局参数调整边距或使用 setX/setY 方法。看看这里 如何动态设置Android 中视图的位置?
The problem may be in the way you're setting the text color. setTextColor() takes a color value argument, not a resource id. You should call
If you want programmatically change your subview position you can adjust margins via layout params or use setX/setY methods. Take a look here How can I dynamically set the position of view in Android?