【问题标题】:How to set width and height of cell item in GridView programmatically in Android如何在Android中以编程方式设置GridView中单元格项目的宽度和高度
【发布时间】:2018-07-11 06:55:38
【问题描述】:

我正在尝试设置 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 列,但单元格的宽度被拉伸 - 高度看起来不错 - 如下所示:

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

【问题讨论】:

    标签: android arrays gridview items


    【解决方案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"/>
    

    【讨论】:

    • 我做到了,不幸的是,这就是我得到的:s6.postimg.cc/4j5t52v1t/Screenshot_2018-07-11-11-07-08.png - 我已经删除了下面的代码 // 根据使用的设备设置列数
    • PS:我已经编辑了你的答案,这就是我解决问题的方法,无论如何非常感谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 2012-03-02
    • 2012-12-08
    • 1970-01-01
    • 2016-10-27
    • 2011-03-09
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多