如何调整图像大小以适应gridview上的imageview?

时间:2016-10-27 04:16:11

标签: java android gridview

我想将照片调整为我在下面拍摄的屏幕截图。但是,我不知道如何解决这个错误。有人可以帮我吗?

java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

GridViewAdapter.java

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
    //            holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
            holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
            DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
            int width = metrics.widthPixels / 3;
            int height = metrics.heightPixels / 5;
            holder.imageView.setLayoutParams(new GridView.LayoutParams(width, height));
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        GridItem item = mGridData.get(position);
    //        holder.titleTextView.setText(Html.fromHtml(item.getTitle()));

        Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
        return row;
    }

grid_item_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:background="@drawable/grid_color_selector"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="100dp"
        android:scaleType="fitCenter"
        android:layout_height="100dp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

将Gridlayout更改为Linearlayout

 holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(width, height));