gpt4 book ai didi

android - 如何在 GridView 中设置图像大小

转载 作者:行者123 更新时间:2023-11-29 15:17:04 25 4
gpt4 key购买 nike

我有一个 GridView ,其中 3 列包裹在相对布局中。目前在大型设备(7 英寸平板电脑)上的 View 看起来很小,所以我想根据屏幕尺寸增加图像的尺寸。

Gridview 的 baseApdter 中我的 getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppInfo entry = mListAppInfo.get(position);

//create a new ImageView and TextView for each item refernced by teh adpter


if(convertView == null) {

//if its not recycled, intilise some attributs

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.layout_appinfo, null);
}

//want to change the soze of the image according to screensize then place in the gridview with this apdter


ImageView ivIcon = (ImageView)convertView.findViewById(R.id.ivIcon);

//ivIcon.setLayoutParams(new GridView.LayoutParams(
//(int)mContext.getResources().getDimension(R.dimen.widthImageGridview),
//(int)mContext.getResources().getDimension(R.dimen.heihgtImageGridview)));
ivIcon.setLayoutParams(new RelativeLayout.LayoutParams(85, 85));
ivIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
ivIcon.setPadding(8, 8, 8, 8);
ivIcon.setImageBitmap(entry.getIcon());

TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
tvName.setText(entry.getName());
tvName.setTextColor(Color.WHITE);
return convertView;
}

还有我的带有 GridView 的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".Main" >

<GridView
android:id="@+id/gvMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="200dip"
android:gravity="center"
android:horizontalSpacing="40dip"
android:numColumns="3"
android:padding="10dip"
android:stretchMode="columnWidth"
android:verticalSpacing="10dip" />

<Button
android:id="@+id/btn_facebook"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/fb_button" />

</RelativeLayout>

尝试使用 imageView.setLayoutparams(...) 抛出

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

最佳答案

异常是由您在此处的代码引起的:

ivIcon.setLayoutParams(new RelativeLayout.LayoutParams(85, 85));

更改为:

ivIcon.setLayoutParams(new LinearLayout.LayoutParams(85, 85));

因为您在 layout_appinfo.xml 中使用了 LinearLayout。当您为任何对象设置布局参数时,它取决于父布局类型。

关于android - 如何在 GridView 中设置图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23204755/

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