android - ImageViews 中加载 Picasso 的图片在 GridLayout 中溢出?

标签 android imageview android-gridlayout

我可以通过以编程方式在 GridLayout 中设置 ImageView 的背景颜色来使 UI 看起来像这样。 enter image description here

我正在使用 Picasso 从这些网格中的服务器加载图像。图像正在拉伸(stretch)以超出其定义的 colspan 和 rowspan。为什么我得到这个? enter image description here

网格布局 xml

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tableGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="0dp"
    app:alignmentMode="alignBounds"
    app:columnOrderPreserved="true"
    app:rowOrderPreserved="true"
    app:useDefaultMargins="false"/>`

还有我在Activity中的网格设置方法

 private void setGrids(int totalRows, int totalCols) {

    int total = gridList.size();
    gridLayout.setColumnCount(totalCols);
    gridLayout.setRowCount(totalRows);
    for (int i = 0; i < total; i++) {

        float rowWeight = Float.parseFloat(gridList.get(i).getRowSpan() + "F");
        float colWeight = Float.parseFloat(gridList.get(i).getColSpan() + "F");

        GridLayout.Spec rowSpan = GridLayout.spec(gridList.get(i).getStartRow(), gridList.get(i).getRowSpan(), rowWeight);
        GridLayout.Spec colSpan = GridLayout.spec(gridList.get(i).getStartColumn(), gridList.get(i).getColSpan(), colWeight);

        GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                rowSpan, colSpan);

        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = 0;
        param.width = 0;
        ImageView oImageView = new ImageView(this);
        oImageView.setLayoutParams(new GridLayout.LayoutParams(param));

       /*
        Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        oImageView.setBackgroundColor(color);
        */

        Picasso.with(this).load(gridList.get(i).getProductImage()).noFade().into(oImageView);
        oImageView.setAdjustViewBounds(true);
        oImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        gridLayout.addView(oImageView, gridParam);
    }
}

指导我,以便我可以将图像正确地放入这些网格中。

最佳答案

我解决了我的问题。 问题在于使用 Picasso 加载图像。图书馆正在调整我的形象。 我将图像加载行更改为

 Picasso.with(this).load(gridList.get(i).getProductImage()).fit().noFade().into(oImageView);

关于android - ImageViews 中加载 Picasso 的图片在 GridLayout 中溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039534/

相关文章:

android - 如何在 LinearLayout 单元格中对齐两个 TextView?

android - 在 Windows 中从 Jenkins 启动 android 模拟器 adb(运行仪器测试,模拟器不会出现)

java - 如何在运行时创建/更新/加载 HTML 文件

android - 平板电脑 3.0 中的滑动手势

android - 如何显示修剪后的 ImageView,使高度等于宽度?

android - 带有正方形子项的网格布局

安卓。在 listView 中搜索

android - 将图像从 ImageView 保存到设备图库

Android:强制更新布局

android - 如何通过代码(动态)将子级添加到GridLayout?