动态设置布局参数

时间:2014-03-17 15:29:19

标签: android layout camera params

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

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

这是代码的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;
            }
        }
    }

不得不说这里的值

RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
如果我改变它,

会更新。不改变的是包含addRule()方法的内容

5 个答案:

答案 0 :(得分:9)

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

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

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

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

relativeLayout.addView(View, Params);

就是这个。

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

试试这个

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

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

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

答案 3 :(得分:0)

尝试将btnTakePhoto的父级设置为mPreview

btnTakePhoto.setParent(mPreview);

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

答案 4 :(得分:0)

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

您必须定位超过14的API,setParent应该使用较新的API。上面的代码是一种替代方法,但是您必须按代码放置所有内容,但它可以正常工作。希望这会有所帮助。

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