在膨胀的相对布局上添加边距

Add margin on inflated relativelayout

我在为膨胀的相对布局设置边距时遇到问题。 这是代码:

for(Produit prod : GlobalVariables.getInstance().getPanier())
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layoutToInflate = (RelativeLayout)inflater.inflate(R.layout.cart_product_layout, null);

            layoutToInflate.setTag(prod);

            ManagedNetworkImageView prodPicture = (ManagedNetworkImageView) layoutToInflate.findViewById(R.id.productImage);
            prodPicture.setImageUrl(prod.getImageDefaultUri(), GlobalVariables.getInstance().getImageLoader());

            ImageView addBtn = (ImageView) layoutToInflate.findViewById(R.id.addBtn);
            ImageView removeBtn = (ImageView) layoutToInflate.findViewById(R.id.removeBtn);
            //Ajouter les actions sur les boutons de gestion de panier.

            TextView productTitle = (TextView) layoutToInflate.findViewById(R.id.productTitle);
            productTitle.setText(prod.getNom());

            TextView productSpecs = (TextView) layoutToInflate.findViewById(R.id.productSpecs);
            //Ajouter les spec choisi par l'utilisateur.

            TextView productPrice = (TextView) layoutToInflate.findViewById(R.id.priceTextView);
            productPrice.setText(Float.toString(prod.getPrixTtc()));

            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            relativeParams.setMargins(0, 10, 0, 0);  // left, top, right, bottom
            layoutToInflate.setLayoutParams(relativeParams);


            productInCartContainer.addView(layoutToInflate);

        }

目标是将列表中包含的对象尽可能多的布局扩展到垂直方向的 LinearLayout(productInCartContainer --> 父视图)中。所有布局都必须以 10 的边距分隔....我不知道我做错了什么,因为根本没有边距...我看到很多关于这个主题的 post 但没有任何效果...有人看到我做错了什么吗?

提前致谢!

我刚刚找到了解决方案。指出我们想在根布局中的哪个位置添加膨胀布局,解决了这个问题。没有指出,只有左边距和右边距有效。由于 post:

我找到了解决方案

Solution is here...

无论如何,我想再次添加整个解决方案,因为每个人都可以通知更改:

int index = 0;
        for(Produit prod : GlobalVariables.getInstance().getPanier())
        {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layoutToInflate = (RelativeLayout)inflater.inflate(R.layout.cart_product_layout, null);

            layoutToInflate.setTag(prod);

            ManagedNetworkImageView prodPicture = (ManagedNetworkImageView) layoutToInflate.findViewById(R.id.productImage);
            prodPicture.setImageUrl(prod.getImageDefaultUri(), GlobalVariables.getInstance().getImageLoader());

            ImageView addBtn = (ImageView) layoutToInflate.findViewById(R.id.addBtn);
            ImageView removeBtn = (ImageView) layoutToInflate.findViewById(R.id.removeBtn);
            //Ajouter les actions sur les boutons de gestion de panier.

            TextView productTitle = (TextView) layoutToInflate.findViewById(R.id.productTitle);
            productTitle.setText(prod.getNom());

            TextView productSpecs = (TextView) layoutToInflate.findViewById(R.id.productSpecs);
            //Ajouter les spec choisi par l'utilisateur.

            TextView productPrice = (TextView) layoutToInflate.findViewById(R.id.priceTextView);
            productPrice.setText(Float.toString(prod.getPrixTtc()));

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(10, 20, 10, 0);
            layoutToInflate.setLayoutParams(params);

            productInCartContainer.addView(layoutToInflate, index);
            index++;

        }