Android 动态设置布局参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22458468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:59:09  来源:igfitidea点击:

Set layout params dynamically

androidlayoutcameraparams

提问by masmic

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

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

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

为此,我正在尝试设置它们的参数,但它们始终出现在屏幕的左上角。

This is the onCreate method of the code:

这是代码的 onCreate 方法:

@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;
            }
        }
    }

Have to say that the values here

不得不说这里的价值观

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

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

如果我改变它们,更新得很好。不变的是包含 addRule() 方法的内容

回答by masmic

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.

终于解决了。在执行 setContentView() 和 addContentView() 时,我将视图放置在作为 FrameLayout 的 DecorView 中。因此,引用 RelativeLayout 的 LayoutParams 将不起作用,至于 FrameLayout 只有 LayoutParams 的通用功能才起作用。

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

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

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

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

但是,现在,每次我想添加一个视图时,我都必须以这种方式将它添加到这个 relativeLayout 中:

relativeLayout.addView(View, Params);

Just this.

只是这个。

回答by Weibo

are you wanna to add the btnTakPhoto to the right center of the view? if so, have a try:

您想将 btnTakPhoto 添加到视图的右中心吗?如果是这样,请尝试:

btn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

回答by RizN81

Try This

尝试这个

// create main linearLayout and set properties
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setBackgroundColor(Color.TRANSPARENT);

        // Create camera layout params
        LinearLayout.LayoutParams cameralayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        // set layout params
        linearLayout.setLayoutParams(cameralayoutParams);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        int m_Height=ScreenHeight(this);
        int buttonWH = 80;

        // Create button for capture, save and discard
        captureButton = new Button(this);
        captureButton.setBackgroundResource(R.drawable.camera);
        captureButton.setWidth(buttonWH);
        captureButton.setHeight(buttonWH);

        saveButton = new Button(this);
        saveButton.setBackgroundResource(R.drawable.save);
        saveButton.setVisibility(View.INVISIBLE);
        saveButton.setWidth(buttonWH);
        saveButton.setHeight(buttonWH);

        discardButton = new Button(this);
        discardButton.setBackgroundResource(R.drawable.discard);
        discardButton.setVisibility(View.INVISIBLE);
        discardButton.setWidth(buttonWH);
        discardButton.setHeight(buttonWH);

        // Create layout for controls
        controlLayout = new LinearLayout(this);
        controlLayout.setOrientation(LinearLayout.VERTICAL);
        controlLayout.setBackgroundColor(Color.BLACK);

        // Create params for control layout
        LinearLayout.LayoutParams controlLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);

        // Set layout params
        controlLayout.setLayoutParams(controlLayoutParams);

        int buttonMargin = ((m_Height - (buttonWH * 3)) / 3) / 2;

        // Create params for capture button
        LinearLayout.LayoutParams buttonCaptureParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        buttonCaptureParams.setMargins(10, buttonMargin, 10, buttonMargin);

        LinearLayout.LayoutParams buttonSaveParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        buttonSaveParams.setMargins(10, buttonMargin, 10, buttonMargin);

        LinearLayout.LayoutParams buttonDiscardParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        buttonDiscardParams.setMargins(10, buttonMargin, 10, buttonMargin);

        // Add button to main layout
        controlLayout.addView(discardButton, buttonDiscardParams);
        controlLayout.addView(captureButton, buttonCaptureParams);
        controlLayout.addView(saveButton, buttonSaveParams);

        // Make it full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // set main layout as content view
        setContentView(linearLayout);

Method to get screen height and width

获取屏幕高度和宽度的方法

public static int ScreenHeight(Context ctx) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }       
public static int ScreenWidth(Context ctx) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.widthPixels;
    }

回答by Liam Neil Parker

ScrollView sv = new ScrollView(this);
this.addContentView(sv, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));
RelativeLayout FORM = new RelativeLayout(this);
sv.addView(FORM, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));

You must be targeting an API older than 14, setParent should work with newer API's. Above code is an alternative way, however you would have to place everything by code, but it works. Hope this helps.

您必须针对 14 岁以上的 API,setParent 应该使用较新的 API。上面的代码是另一种方式,但是您必须按代码放置所有内容,但它有效。希望这可以帮助。

FORM is the container, and you would have to add your other components like FORM.addView(btn) with relative layout params.

FORM 是容器,您必须使用相对布局参数添加其他组件,例如 FORM.addView(btn)。

回答by Liam Neil Parker

try to set the parent of btnTakePhoto to mPreview

尝试将 btnTakePhoto 的父级设置为 mPreview

btnTakePhoto.setParent(mPreview);

which will cause the button to appear above the preview.

这将导致按钮出现在预览上方。