Set layout params dynamically

2019-03-31 20:15发布

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.

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

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

5条回答
家丑人穷心不美
2楼-- · 2019-03-31 20:24

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

btn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
查看更多
Juvenile、少年°
3楼-- · 2019-03-31 20:32

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;
    }
查看更多
Explosion°爆炸
4楼-- · 2019-03-31 20:37
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.

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

查看更多
可以哭但决不认输i
5楼-- · 2019-03-31 20:39

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.

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);

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

relativeLayout.addView(View, Params);

Just this.

查看更多
叛逆
6楼-- · 2019-03-31 20:39

try to set the parent of btnTakePhoto to mPreview

btnTakePhoto.setParent(mPreview);

which will cause the button to appear above the preview.

查看更多
登录 后发表回答