java - Center the content of a GridView -
my gridview
centered the content of not centered within it. here screen, light blue on bottom , right background color set of gridview
.
you can see content pushed top , left. content, game board, centered in middle of gridview
, light blue background color surrounding sides equally.
here xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textfieldfu" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <gridview android:id="@+id/gridview" android:layout_margintop="0dp" android:layout_marginleft="45dp" android:layout_marginright="45dp" android:layout_width="fill_parent" android:layout_height="485dp" android:gravity="center" android:horizontalspacing="0dp" android:numcolumns="8" android:background="@android:color/holo_blue_bright" android:verticalspacing="0dp" />
and getview
in imageadapter
class if helps:
@override public view getview(int position, view convertview, viewgroup parent) { imageview iv; if (convertview != null) { iv = (imageview) convertview; } else { iv = new imageview(context); iv.setlayoutparams(new gridview.layoutparams(60, 60)); iv.setscaletype(scaletype.fit_center); iv.setpadding(0, 0, 0, 0); if(position < 8 && position % 2 ==0){ iv.setbackgroundcolor(color.dkgray); } else if(position > 7 && position < 16 && position % 2 ==1){ iv.setbackgroundcolor(color.dkgray); } else if(position > 15 && position < 24 && position % 2 ==0){ iv.setbackgroundcolor(color.dkgray); } else if(position > 23 && position < 32 && position % 2 ==1){ iv.setbackgroundcolor(color.dkgray); } else if(position > 31 && position < 40 && position % 2 ==0){ iv.setbackgroundcolor(color.dkgray); } else if(position > 39 && position < 48 && position % 2 ==1){ iv.setbackgroundcolor(color.dkgray); } else if(position > 47 && position < 56 && position % 2 ==0){ iv.setbackgroundcolor(color.dkgray); } else if(position > 55 && position < 64 && position % 2 ==1){ iv.setbackgroundcolor(color.dkgray); } else iv.setbackgroundcolor(color.gray); } iv.setimageresource(images[position]); return iv; }
try 1 of these options creating border around view. avoid arbitrary fixed values 45dp margins , 485dp height, not right on other screen sizes.
also, suggest use gridlayout instead of gridview. technically, requires api level 14 can use 1 in support library support older versions of android. won't trivial change, because need add tiles calling addview
instead of overriding getview
method.
gridlayout more appropriate drawing grids have fixed number of tiles displayed. gridview more complex widget has lot of internal logic loading tiles lazily, don't need here. may make layouts easier well.
Comments
Post a Comment