动态设置布局参数 [英] Set layout params dynamically

查看:134
本文介绍了动态设置布局参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CameraPreview示例API演示。我需要添加一些视图(按钮等)覆盖SurfaceView。

I'm using the CameraPreview example API demo. I need to add some views (button, etc..) overlaying the SurfaceView.

为此,我试图设置他们的参数,但他们出现所有的时间

For this, I'm trying to set their parameters, but they appear all the time on the top-left side of the screen.

这是代码的onCreate方法:

This is the onCreate method of the code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        btnTakePhoto = new Button(this);
        btnTakePhoto.setBackgroundResource(android.R.drawable.ic_menu_camera);


        /*Set container*/
        mPreview = new Preview(this);
        setContentView(mPreview);

        /*Set button params and add it to the view*/
        RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
        addContentView(btnTakePhoto, buttonParams);


        numberOfCameras = Camera.getNumberOfCameras();

        CameraInfo cameraInfo = new CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                defaultCameraId = i;
            }
        }
    }

RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

正在更新。什么不改变是什么包含addRule()方法

are updating well if I change them. What doesn't change is what contains the addRule() method

推荐答案

当做setContentView()和addContentView(),我把视图放在一个DecorView是一个FrameLayout。因此,引用RelativeLayout的LayoutParams将不起作用,因为对于FrameLayout,只有LayoutParams的泛型功能才能工作。

Finally solved. When doing setContentView() and addContentView(), I was placing the views in a DecorView which is a FrameLayout. So, LayoutParams referencing RelativeLayout won't work, as for a FrameLayout only generic features of LayoutParams will work.

所以,事情是先创建一个relativeLayout,设置params并将其设置为内容:

So, the thing is to first create a relativeLayout, set the params and set it as the content:

RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(relativeLayout, rlp);

但是,现在,每次我想添加一个视图,我必须将它添加到这个relativeLayout这样:

But, now, every time I want to add a view, I have to add it to this relativeLayout this way:

relativeLayout.addView(View, Params);

只是这样。

这篇关于动态设置布局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆