GridView.LayoutParams在不同的屏幕尺寸中是相同的,这有可能吗? [英] GridView.LayoutParams to be the same in different screen sizes, is it possible?

查看:475
本文介绍了GridView.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/>

继承了布局的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;
}

我尝试将v.setLayoutParams ...更改为

  v.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 

  v.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 

但是这两个让GridView无法使用。如果有人有任何想法我做错了什么,请告诉我,如果有人需要别的东西也要求它(我不能张贴截图)

解决方案

问题在于,您正在对单元格的宽度和高度进行硬编码。

这使得所有手机上的像元宽度都为80像素。如果手机具有更大的分辨率(真实像素变小),您的视角会更小。如果您真的想在Java中创建整个视图,而不是在xml中创建至少加载 dimension 。这将使您能够保存密度独立像素的值,并让手机将该值调整为与实际屏幕分辨率相匹配的值。

阅读支持多个屏幕部分,以便更深入地了解这一点。


I made a Grid View with 6 columns and 60 rows. Where I can add, move and erase images. But I'm having trouble to set the distance between columns (I don't want any space between columns). Since it will change deppending on screen size. I set it up for my phone, then tried it at a friends phone and there where like 10dp between columns. Heres the xml for the GridView EDIT: and if I try it in a smaller phone the images where to big to fit the cell.

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

Heres the java code for the layout

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;
}

I tried changing that v.setLayoutParams... to

v.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ));

and

v.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

But those two maked the GridView unable to use. If anyone have any idea of what am I doing wrong please tell me, if someone needs something else also ask for it (I can't post screenshots)

解决方案

The problem is that you are hardcoding the width and height for your cells.

This makes the cells 80 pixel wide on all phones. If the phone has a bigger resolution (the real pixels get smaller) your view will be smaller. If you really want to create the whole view in Java and not in xml at least load the dimension for your cells from a ressource file. This will enable you to save the value in density independent pixel and have the phone adjust the value to something that matches the actual screen resolution.

Read the supporting multiple screens sections in the documentation for more insight into this.

这篇关于GridView.LayoutParams在不同的屏幕尺寸中是相同的,这有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆