0

好吧,我有以下代码,它加载 5 张图片和 5 个复选框,每张图片一个:

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

第一个复选框和图像在他们的位置。问题在于其他图像比第一张图片略低。除了第一张图片,它们都对齐了,但低于它们应有的位置(它们应该与第一张图片对齐)。我做错了什么?

4

1 回答 1

0

解决方法: if (cb.getId() == 0) { imageParams.topMargin = 72; }

于 2012-08-14T13:44:36.553 回答