我正在以编程方式在 ImageView
中添加 LinearLayout
:
for (int i = 0; i < length; i++) {
final ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth / 2, screenHeight / 3);
params.setMargins(10, 10, 10, 10); //SETTING MARGIN HERE
imageView.setLayoutParams(params);
mBrandLinearLayout.addView(imageView); // adding in LinearLayout
Picasso.with(mContext)
.load(mBrandArrayList.get(i).getBrandLogo())
.resize(screenWidth / 2, screenHeight / 2)
.placeholder(R.drawable.ic_photo_24dp)
.error(R.drawable.ic_photo_24dp)
.into(imageView);
}
如果不使用 Picasso 加载图片,它可以使用可绘制图片的样本,但在添加 Picasso 代码后,它会删除顶部和底部边距。
注意:我尝试先加载图像并在加载后设置图像 图像,但它也无法正常工作。
有没有办法用 Picasso 设置边距?
答案 0 :(得分:1)
在
中添加 -10 解决了问题 new LinearLayout.LayoutParams(screenWidth / 2, screenHeight / 3 - 10);
和
<强> .resize(screenWidth / 2, screenHeight / 3 - 10)
强>
for (int i = 0; i < length; i++) {
final ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(screenWidth / 2, screenHeight / 3 - 10); //ADDED -10
params.setMargins(5, 5, 5, 5);
imageView.setLayoutParams(params);
mBrandLinearLayout.addView(imageView);
Picasso.with(mContext)
.load(mBrandArrayList.get(i).getBrandLogo())
.resize(screenWidth / 2, screenHeight / 3 - 10) //ADDED -10
.placeholder(R.drawable.ic_photo_24dp)
.error(R.drawable.ic_photo_24dp)
.into(imageView);
}