GridView.LayoutParams to be the same in different screen sizes, is it possible?

穿精又带淫゛_ 提交于 2019-12-08 04:28:21

问题


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)


回答1:


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.




回答2:


I had a similar problem, and i used another approach with 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. :-)




回答3:


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:

<GridView
        ...
        android:stretchMode="none"
        android:horizontalSpacing="0dp"
</GridView>



回答4:


See 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).



来源:https://stackoverflow.com/questions/10329673/gridview-layoutparams-to-be-the-same-in-different-screen-sizes-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!