以编程方式将子视图添加到 FrameLayout,然后更改它们的位置

发布于 2024-11-24 21:29:46 字数 1216 浏览 3 评论 0原文

我正在做一个增强现实应用程序,我需要在相机视图上显示一些对象。我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅士风度i 2024-12-01 21:29:46

问题可能出在您设置文本颜色的方式上。 setTextColor() 采用颜色值参数,而不是资源 ID。 如果您想以编程方式更改子视图位置,您应该调用,

fs.setTextColor(ContextCompat.getColor(context, android.R.color.white));

您可以通过布局参数调整边距或使用 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

fs.setTextColor(ContextCompat.getColor(context, android.R.color.white));

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文