【发布时间】: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