现在,我正在努力完成一些简单的事情,比如在自定义 LinearLayout 中添加我的子 ImageViews 之间的边距空间(修改后的 RadioGroup 旨在接收实现 Checkable 的自定义 ImageView,没有覆盖 onMesarue)。长话短说,这些图像的尺寸是固定的(60x60dip),由于它们是动态的(来自网络),我不得不像这样动态地添加它们:
for(int i = 0; i < num; i++){
ImageViewRadioButton childImage = new ImageViewRadioButton(mContext);
float imagehWidthHeight = getResources().getDimension(R.dimen.image_width_and_height);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams((int) imageWidthHeight, (int) imageWidthHeight);
int imageSpacing = Utils.dipsToPixels(10, mContext);
int innerPadding = Utils.dipsToPixels(5, mContext);
imageParams.leftMargin = imageSpacing;
imageParams.rightMargin = imageSpacing;
childImage.setAdjustViewBounds(true);
childImage.setLayoutParams(imageParams);
childImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_pressed));
childImage.setPadding(innerPadding, innerPadding, innerPadding, innerPadding);
childImage.setClickable(true);
//other non-image properties...
imageContainer.addView(childImage);
}
唯一有效的是填充,它可以正确地将其隔开。但是,我没有看到每个孩子的填充(边距)之间有任何空间。我这样做是否正确,或者有没有更好的方法来代替 onMeasure 以考虑每个孩子的利润?