Android - ImageView边距不出现在LinearLayout
问题描述:
现在,我努力完成一些任务,就像在自定义的LinearLayout中添加我的子ImageView之间的空白区域一样简单(修改后的RadioGroup设计用于实现可检查的自定义ImageView ,没有覆盖在梅萨里)。长话短说,这些图像是一个固定的尺寸(60x60dip),并且因为它们是动态的(从网站),我必须动态地添加他们像这样:Android - ImageView边距不出现在LinearLayout
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);
}
,做工作的唯一的事情是填充,它将其正确地分隔开。但是,我没有看到每个孩子的填充(边距)之间有任何空间。我是否正确地做了这件事,还是有没有更好的方法来做到这一点,而不是为了考虑每个孩子的利润率?
答
您已经创建了imageParams
,但是您没有在代码中使用该参数,而是使用swatchParams
参数的imageParams
。而且你还没有输入swatchParams
参数的代码。
显然,这是一个例子,所以这应该是imageParams。即使那时我创建了LayoutParams对象并将其传递到视图中,它仍然无法工作。 –