我需要更改 Drawable[] 的第二层的大小。我有以下代码:
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 / 3);
final int itemHeight = (height / 3);
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());
if (cb.getId() == 0) {
imageParams.topMargin = 72;
}
parent.addView(cb, checkBoxParams);
parent.addView(iv, imageParams);
hLayout.addView(parent);
//ImageUtil.loadImage(coupon.getImage(), iv, itemWidth, itemHeight, "", false, getActivity());
Bitmap b =Util.generateEAN("9310779300005", getActivity());
Drawable d = new BitmapDrawable(getResources(),b);
System.out.println("------------b : "+b);
//iv.setImageBitmap(b);
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.coupon1);
layers[1] = d;
layers[1].setAlpha(200);
LayerDrawable layerDrawable = new LayerDrawable(layers);
iv.setImageDrawable(layerDrawable);
}
}
}
}
}, 200);
我尝试使用 layers[1].setBounds(left, top, right, bottom); 但没有运气,我不知道还能尝试什么。