【问题标题】:Set layout params dynamically动态设置布局参数
【发布时间】:2014-03-18 17:54:38
【问题描述】:

我正在使用 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() 方法的内容

【问题讨论】:

    标签: android layout camera params


    【解决方案1】:

    终于解决了。在执行 setContentView() 和 addContentView() 时,我将视图放置在一个 FrameLayout 的 DecorView 中。因此,引用 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);
    

    就这个。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我必须在哪里设置?如果我在 addContentView 之前这样做,则无法正常工作
      【解决方案3】:

      试试这个

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

      【讨论】:

        【解决方案4】:

        尝试将 btnTakePhoto 的父级设置为 mPreview

        btnTakePhoto.setParent(mPreview);
        

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

        【讨论】:

        • 错误:方法 setParent(Preview) 未定义类型按钮
        【解决方案5】:
        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)。

        【讨论】:

        • 不,我不是。我的目标是 API 19。我在代码上尝试了一些 mods,按钮以这种方式显示,但没有使用规则,按钮始终显示在左上角。我真正需要的是使工作符合 CERTER-VERTICAL 之类的规则,
        猜你喜欢
        • 2012-02-23
        • 2013-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 2011-09-21
        • 1970-01-01
        相关资源
        最近更新 更多