【问题标题】:Add margin on inflated relativelayout在膨胀的相对布局上添加边距
【发布时间】:2015-09-08 16:05:43
【问题描述】:

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

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....有人看到我做错了吗?

提前致谢!

【问题讨论】:

  • 使用 productInCartContainer 作为 inflate 方法的第二个参数膨胀并添加 RelativeLayout(您当前传递 null)。
  • 如果上面的方法不行,你也可以试试MarginLayoutParams
  • 我刚刚尝试过,当我在最后一行添加膨胀布局时,它给了我一个错误。它说父母已经有一个孩子。
  • 使用我上面所说的 inflate 方法意味着您已经将 RelativeLayout 添加到容器中。
  • 感谢它有效,但不适用于下边距或上边距......所有布局都粘在一起

标签: android android-layout layout-inflater margins


【解决方案1】:

我刚刚找到了解决方案。指出我们想在根布局中添加膨胀布局的位置,解决了这个问题。在没有说明的情况下,只有左右边距有效。由于该帖子,我找到了解决方案:

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

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多