java GridView.LayoutParams 在不同的屏幕尺寸下是相同的,这可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10329673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridView.LayoutParams to be the same in different screen sizes, is it possible?
提问by R-Roadster
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.
我制作了一个 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" />
Heres the java code for the layout
这是布局的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;
}
I tried changing that v.setLayoutParams... to
我尝试将 v.setLayoutParams... 更改为
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)
但是这两个使得GridView无法使用。如果有人知道我做错了什么,请告诉我,如果有人需要其他东西也可以要求它(我无法发布屏幕截图)
回答by Rafael Gutiérrez
I had a similar problem, and i used another approach with getDisplayMetrics.
我有一个类似的问题,我使用了另一种方法与 getDisplayMetrics。
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
And divide between number of columns.
并在列数之间进行划分。
v.setLayoutParams(new GridView.LayoutParams(screenWidth/2, screenWidth/2));
v.setPadding(8, 8, 8, 8);
It's not so elegant, but its easy. :-)
它不是那么优雅,但很容易。:-)
回答by Janusz
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 dimensionfor 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.
这使得所有手机上的单元格宽度为 80 像素。如果手机具有更大的分辨率(实际像素变小),您的视图会更小。如果您真的想用 Java 而不是 xml 创建整个视图,至少从资源文件中加载单元格的维度。这将使您能够以与密度无关的像素保存值,并让手机将值调整为与实际屏幕分辨率相匹配的值。
Read the supporting multiple screenssections in the documentation for more insight into this.
阅读文档中的支持多屏幕部分以更深入地了解这一点。
回答by Aleks G
In addition to Janusz's comment, you may need to set your stretchMode
to none
so that Android would not stretch anythings automatically. Further, if you don't need to have any spacing between columns, then set horizontalSpacing
to 0. You may end up with this XML:
除了 Janusz 的评论之外,您可能还需要将您设置stretchMode
为 ,none
以便 Android 不会自动拉伸任何内容。此外,如果您不需要列之间的任何间距,则设置horizontalSpacing
为 0。您可能会得到以下 XML:
<GridView
...
android:stretchMode="none"
android:horizontalSpacing="0dp"
</GridView>
回答by Thkru
See http://developer.android.com/guide/practices/screens_support.html
请参阅 http://developer.android.com/guide/practices/screens_support.html
If your graphics are too big on small phones use additional drawable directories like drawable-mdpi or sth.
Also u should try android:stretchMode="none"
in your gridview (xml).
如果您的图形在小型手机上太大,请使用其他可绘制目录,例如 drawable-mdpi 或 sth。你也应该android:stretchMode="none"
在你的 gridview (xml) 中尝试。