繁体   English   中英

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

[英]Adding children views to a FrameLayout programmatically, then change their positions

我正在做一个增强现实应用程序,我需要在相机视图上显示一些对象。 我正在创建一个 FrameLayout,向其中添加我的相机视图,然后添加我的 AR 对象(现在我正在尝试使用 TextView)。 在我添加到我的 FrameLayout 的 TextView 下面的代码中没有显示...

有人可以帮忙吗? 一旦显示fs ,我如何以编程方式更改其 position?

更新

我尝试使用 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);

    }    
}

问题可能出在您设置文本颜色的方式上。 setTextColor() 采用颜色值参数,而不是资源 ID。 你应该打电话

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

如果您想以编程方式更改子视图 position 您可以通过布局参数调整边距或使用 setX/setY 方法。 看看这里如何在Android中动态设置视图的position?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM