简体   繁体   中英

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

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)

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

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM