gpt4 book ai didi

java - 如何调整图像大小以适合gridview上的imageview?

转载 作者:行者123 更新时间:2023-12-01 09:19:18 24 4
gpt4 key购买 nike

我想将图片大小调整为下面捕获的屏幕截图。但是,我不知道如何解决这个错误。有人可以帮我解决这个问题吗?

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>

最佳答案

将网格布局更改为线性布局

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

关于java - 如何调整图像大小以适合gridview上的imageview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40276017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com