如何在 Android 中以编程方式设置 GridView 中单元格项目的宽度和高度

How to set width and height of cell item in GridView programmatically in Android

我正在尝试设置 GridView 项目的动态宽度和高度,这是我的代码:

 class GridAdapter extends BaseAdapter {
            private Context context;
            private GridAdapter(Context context, List<ParseObject> objects) {
                super();
                this.context = context;
            }

            // CONFIGURE CELL
            @Override
            public View getView(int position, View cell, ViewGroup parent) {
                if (cell == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    cell = inflater.inflate(R.layout.cell_post_search, null);
                }

                //-----------------------------------------------
                // MARK - INITIALIZE VIEWS
                //-----------------------------------------------
                ImageView postImg = cell.findViewById(R.id.cpsPostImg);
                ImageView videoicon = cell.findViewById(R.id.cpsVideoIcon);

            ...

            return cell;
            }
                @Override public int getCount() { return postsArray.size(); }
                @Override public Object getItem(int position) { return postsArray.get(position); }
                @Override public long getItemId(int position) { return position; }
        }

        // Set Adapter
        postsGridView.setAdapter(new GridAdapter(ctx, postsArray));

        // Set number of Columns accordingly to the device used
        float scalefactor = getResources().getDisplayMetrics().density * screenW/3; // LET'S PRETEND MY screenW = 720, this value whoudl, be 240, which is the width i need for my cell

        int number = getWindowManager().getDefaultDisplay().getWidth();
        int columns = (int) ((float) number / scalefactor);
        postsGridView.setNumColumns(columns);
        Log.i(Configurations.TAG, "SCALE FACTOR: " + scalefactor);

这是我的习惯 cell_post_search.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/cpsCellLayout"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <ImageView
            android:id="@+id/cpsPostImg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="#c1c1c1"
            android:scaleType="centerCrop"/>

        <ImageView
            android:id="@+id/cpsVideoIcon"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:layout_alignEnd="@+id/cpsPostImg"
            android:layout_alignParentTop="true"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/play_butt"/>

        <ImageView
            android:id="@+id/cpsWhiteFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/white_frame"/>
    </RelativeLayout>
</RelativeLayout>

我在 Logcat 中得到 480.0 作为比例因子,这不是我需要的正确大小(在我的例子中它应该是 240)。无论如何,我也尝试过这样做:

int columns = (int) ((float) number / (scalefactor/2));

所以 scalefactor = 240,但这没关系,因为我需要我的单元格的项目是正方形大小,所以基本上:WIDTH = screenWidth/3, 身高 = screenWidth/3.

它不能正常工作,我的 GridView 显示 3 列,但单元格的宽度被拉伸 - 高度看起来不错 - 如下所示:

有没有办法根据设备大小编辑我的代码并使单元格大小正确,如方形图像、3 列?

试试这个

 class GridAdapter extends BaseAdapter {
        private Context context;
        private GridAdapter(Context context, List<ParseObject> objects) {
            super();
            this.context = context;
        }

        // CONFIGURE CELL
        @Override
        public View getView(int position, View cell, ViewGroup parent) {
            if (cell == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                cell = inflater.inflate(R.layout.cell_post_search, null);
            }

            //-----------------------------------------------
            // MARK - INITIALIZE VIEWS
            //-----------------------------------------------
            ImageView postImg = cell.findViewById(R.id.cpsPostImg);
            ImageView videoicon = cell.findViewById(R.id.cpsVideoIcon);

            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            postImg.getLayoutParams().height = width / 3;

        ...

        return cell;
        }
            @Override public int getCount() { return postsArray.size(); }
            @Override public Object getItem(int position) { return postsArray.get(position); }
            @Override public long getItemId(int position) { return position; }
    }


此外,在您的 XML 布局中,添加 android:numColumns="3":

<GridView
            android:id="@+id/upPostsGridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3"/>