Android - Picasso在加载Image后删除了保证金

时间:2016-03-19 06:10:17

标签: android android-imageview picasso

我正在以编程方式在 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 设置边距

1 个答案:

答案 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);
}