简体   繁体   English

Android /布局-实用地将边距顶部添加到相对布局

[英]Android / Layout - Add margin top pragmatically to Relative Layout

I'm trying to add pragmatically a margin top to my Relative Layout in my activity. 我正在尝试在活动中为我的相对布局实用地添加一个边距顶部。 Using the xml i can do it in this mode: android:layout_marginTop="10dp" , but when i'm trying to do it pragmatically nothing change... 使用xml时,我可以在以下模式下执行此操作: android:layout_marginTop="10dp" ,但是当我尝试进行实际操作时,没有任何变化...

This is the code that i'm using: 这是我正在使用的代码:

//SCROLL VIEW
        ScrollView scrollView = new ScrollView(this);
        scrollView.setBackground(getResources().getDrawable(R.drawable.background));
        scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
                                                     ScrollView.LayoutParams.MATCH_PARENT));
        scrollView.setPadding(20, 20, 20, 20);

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

        RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
        head_params.setMargins(0, 80, 0, 0);
        relativeLayout.setLayoutParams(head_params);

        //Need to understand how put a margin top to the relativeLayout

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

}

I have tried also relativeLayout.setMargins(20, 0, 0, 0); 我也试过relativeLayout.setMargins(20, 0, 0, 0); but seems that this method isn't available for relativeLayout. 但似乎此方法不适用于relativeLayout。 I'm not sure about it. 我不确定。

Thanks 谢谢

Change your setmargins line to 将您的保证金行更改为

head_params.setMargins(0, 80, 0, 0);

Margin parameters go clockwise starting with the left margin, ie (left, top, right, bottom). 保证金参数从左保证金开始顺时针旋转,即(左,上,右,下)。 However, if you have other margins other than the top one, this will delete them. 但是,如果您有除上边距以外的其他边距,这将删除它们。 A much easier and more intuitive way is to simply write 一种更简单,更直观的方法是简单地编写

header_params.topMargin=80;

Also not to forget, you never set your header_params back to your view. 同样不要忘记,您永远不会将header_params设置回视图。 Add this line below: 在下面添加此行:

relativeLayout.setLayourParams(header_params);

Try this 尝试这个

RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)headView.getLayoutParams();
head_params.setMargins(0, 80, 0, 0);//substitute parameters for left, top, right, bottom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM