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

标签 android android-layout gridview

我有一个 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/

相关文章:

java - Android Studio - 文件是只读的

android - 如何在android中使用z-index

java - 如何以编程方式设置RelativeLayout的layout_constraintTop_toBottomOf “parent”? (安卓)

android - 相对布局宽度/高度无法以编程方式工作?

asp.net - 如何使用 VB 将数据库表中的下拉列表数据绑定(bind)到 gridview 中?

android - 如何检测安卓设备是否与安卓 watch 配对

java - 如何在 android 中使用 Room Persistence ORM 工具实现 created_at 和 updated_at 列

android - 与 Facebook Messenger 分享

c# - asp.net 从代码隐藏中手动添加一行

gridview - WinRT XAML GridView 控件中的 "Sticky scrolling"问题