以编程方式向RelativeLayout添加边距

时间:2013-09-17 08:01:43

标签: android android-layout

我正在尝试以编程方式将余量添加到我的RelativeLayout活动中。使用xml我可以在这种模式下执行:android:layout_marginTop="10dp",但是当我尝试以编程方式执行时没有任何更改...正如您所看到的,我正在使用一些RelativeLayout(有一个for loop)在一个LinearLayout容器中。

这是我正在使用的代码:

//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT));

for (int i=1; i<=3; i++){
    //RELATIVE LAYOUT
    RelativeLayout relativeLayout = new RelativeLayout(this);
    relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT));
    relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));

    //CODE FOR ADD MARGINS
    RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
    relativeParams.topMargin=80;
    relativeLayout.setLayoutParams(relativeParams);

    //IMAGE VIEW
    ImageView selectedPhoto = new ImageView(this);
    selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    selectedPhoto.setImageResource(R.drawable.ic_launcher);

    //TEXT VIEWS
    TextView numberCopies = new TextView(this);
    numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    numberCopies.setGravity(Gravity.CENTER);
    numberCopies.setPadding(25, 25, 25, 25);
    numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
    numberCopies.setText("2 copies ");
    RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
    layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
    numberCopies.setLayoutParams(layoutParamsNumberCopies);

    TextView priceCopies = new TextView(this);
    priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    priceCopies.setGravity(Gravity.CENTER);
    numberCopies.setPadding(25, 25, 25, 25);
    priceCopies.setTextColor(getResources().getColor(R.color.redColor));
    priceCopies.setText("$ 25 ");
    RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
    layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    priceCopies.setLayoutParams(layoutParamsPriceCopies);

    relativeLayout.addView(selectedPhoto);
    relativeLayout.addView(numberCopies);
    relativeLayout.addView(priceCopies);
    linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);

我认为失败的块代码是这样的:

//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);

4 个答案:

答案 0 :(得分:52)

得到解决方案。

RelativeLayout内使用LinearLayout时,您需要使用LinearLayout.LayoutParams代替RelativeLayout.LayoutParams

所以请替换以下代码......

RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0);  // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);

...与

// CODE FOR ADD MARGINS
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
linearParams.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(linearParams);
relativeLayout.requestLayout();

答案 1 :(得分:11)

您可以使用setMargins (int left, int top, int right, int bottom)

试试这样..

//CODE FOR ADD MARGINS
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    relativeParams.setMargins(0, 80, 0, 0);
    relativeLayout.setLayoutParams(relativeParams);

Layout params should be set with resp to parentLayout ..在这种情况下,LinearLayout由@ChintanRathod正确指出,所以

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

会做的伎俩..

答案 2 :(得分:4)

请注意,您在线性布局中使用了相对布局,因此您应该使用LinearLayout.LayoutParams

我会替换它:

        //RELATIVE LAYOUT
        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT));
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));

        //CODE FOR ADD MARGINS
        RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
        relativeParams.topMargin=80;
        relativeLayout.setLayoutParams(relativeParams);

由此:

        //RELATIVE LAYOUT WITH PROPER LAYOUT PARAMS TO ADD MARGINS
        RelativeLayout relativeLayout = new RelativeLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 80, 0, 0);
        relativeLayout.setLayoutParams(params);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));

另请注意,您应该使用WRAP_CONTENT作为垂直linearLayout中子项的高度配置。 (在FILL_PARENTMATCH_PARENT模式下,它将填充父级,而不会为其他孩子留出空间)

答案 3 :(得分:1)

//科特林

val relativeParams = relativeLayoutGeneral.layoutParams as FrameLayout.LayoutParams                   
relativeParams.setMargins(0, 7, 0, 111)
relativeLayoutGeneral.layoutParams = relativeParams