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

时间:2018-07-11 06:55:38

标签: android arrays gridview items

我正在尝试设置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 HEIGHT = screenWidth / 3

它无法正常工作,我的GridView显示3列,但是单元格的宽度被拉伸了-高度看起来很好-如下所示:

Stretched width for cells

是否有一种方法可以编辑代码并根据设备的大小正确设置单元格的大小(正方形图像,三列)?

1 个答案:

答案 0 :(得分:1)

尝试一下

 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"/>