GridView.LayoutParams在不同的屏幕尺寸中是相同的,是否可能?

R-R*_*ter 1 java android gridview image layoutparams

我创建了一个包含6列和60行的网格视图.我可以在哪里添加,移动和删除图像.但是我很难设置列之间的距离(我不希望列之间有任何空格).因为它会改变屏幕尺寸的变化.我为我的手机设置了它,然后在朋友的手机上尝试了它,并且在列之间有10dp.下面是GridView 编辑的xml :如果我在一个较小的手机中尝试将图像放大到适合单元格的位置.

<GridView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/image_grid_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:numColumns="@integer/num_columns"
            android:stretchMode="columnWidth" />
Run Code Online (Sandbox Code Playgroud)

下面是布局的java代码

ImageCell v = null;
if (convertView == null) {
    v = new ImageCell (mContext);
    v.setLayoutParams(new GridView.LayoutParams(80, 80));
    v.setScaleType(ImageView.ScaleType.CENTER_CROP );

} else {
    v = (ImageCell) convertView;
}
Run Code Online (Sandbox Code Playgroud)

我尝试将v.setLayoutParams改为

v.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ));
Run Code Online (Sandbox Code Playgroud)

v.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

但是这两个让GridView无法使用.如果有人知道我做错了什么请告诉我,如果有人需要别的东西也要求它(我不能发布截图)

Raf*_*rez 6

我有类似的问题,我使用另一种方法与getDisplayMetrics.

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)

并在列数之间划分.

v.setLayoutParams(new GridView.LayoutParams(screenWidth/2, screenWidth/2));
v.setPadding(8, 8, 8, 8);
Run Code Online (Sandbox Code Playgroud)

它不是那么优雅,但它很容易.:-)