我有以下代码,可以加载5张图片和5张chekbox,每张图片一张:
hLayout.postDelayed(new Runnable() {
DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
@Override
public void run() {
if (coupons != null) {
int coupSize = coupons.size();
final int itemWidth = (width / 2);
final int itemHeight = (height / 2);
hLayout.removeAllViews();
for (int i = 0; i < coupSize ; i++) {
Coupon coupon = coupons.get(i);
if (coupon.getImage() != null) {
RelativeLayout parent = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams linearparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parent.setLayoutParams(linearparams);
final CheckBox cb = new CheckBox(getActivity());
final ImageView iv = new ImageView(getActivity());
//iv.setScaleType(ScaleType.CENTER_INSIDE);
LayoutParams checkBoxParams = new LayoutParams();
cb.setId(i);
checkBoxParams.height = LayoutParams.WRAP_CONTENT;
checkBoxParams.width = LayoutParams.WRAP_CONTENT;
cb.setOnCheckedChangeListener(changeListener);
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(itemWidth, itemHeight);
imageParams.height = itemHeight;
imageParams.width = itemWidth;
imageParams.addRule(RelativeLayout.BELOW, cb.getId());
parent.addView(cb, checkBoxParams);
parent.addView(iv, imageParams);
hLayout.addView(parent);
ImageUtil.loadImage(coupon.getImage(), iv, itemWidth, itemHeight, "", false, getActivity());
}
}
}
}
}, 200);
第一个复选框和图片就在他们的位置。问题在于其他图像比第一张图片略低。除了第一张图片之外,它们都是对齐的,但它们应该更低(它们应该与第一张图片对齐)。我做错了什么?
答案 0 :(得分:0)
解决: if(cb.getId()== 0){ imageParams.topMargin = 72; }