简体   繁体   中英

Add margin on inflated relativelayout

I'm facing an issue setting margin to an inflated relative layout. Here is the code:

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

        }

The goal is to inflate as much layouts as I have objects in my list, into a LinearLayout (productInCartContainer --> The parent view) with vertical orientation. All the layout have to be separated by a margin of 10.... I don't know what I'm doing wrong by there's no margin at all... I saw many post on that subject but nothing works... Does somebody sees what I'm doing wrong ?

Thanks in advance !

I've just found the solution. Indicating at which position we would like to add the inflated layout in the root layout, solves the issue. Without indicating that, only left and right margins were working. I found the solution thanks to that post:

Solution is here...

Anyway, I wanted to add the whole solution again as everybody can notify the changes:

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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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