java - Easy way to make an ImageView that is underneath a GridView match the GridView's size exactly? -
my imageview
displaying image of chess style board little game i'm making. covered gridview
displays images of pieces. have displaying underneath not positioned properly. want match edges of gridview
don't know how. there way make gridview
"parent" of imageview
"match_parent" in xml it's height , width , on?
here screen of how app looks now. want ugly colored grid made line gridview
images black squares around edge of pieces centered in squares of board.
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" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/gridview" android:layout_aligntop="@+id/gridview" android:layout_centerhorizontal="true" /> <gridview android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="600dp" android:gravity="center" android:horizontalspacing="0dp" android:numcolumns="8" android:stretchmode="columnwidth" android:verticalspacing="10dp" />
here relevant part of mainactivity
:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final imageadapter ia = new imageadapter(this); final imageview board; board = (imageview) findviewbyid(r.id.imageview1); board.setimageresource(r.drawable.board1); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(ia); ...
and here relevant parts of imageadapter
have sets gridview
:
public class imageadapter extends baseadapter { public static int[] images = { r.drawable.rock2, r.drawable.paper2, r.drawable.scissors2, r.drawable.rock2, r.drawable.paper2, ...//this part goes on while... ...//on relevant part... @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.center); iv.setpadding(0, 0, 0, 0); } iv.setimageresource(images[position]); return iv; } public static int[] getimagesarray() { return images; } public void setimagesarray(int[] newimages) { images = newimages; }
----------------edit, added alternative solution answer below.--------------
i got rid of imageview
entirely , added getview
in imageadapter
class:
@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.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; }
here how looks now:
you can configure layout of base element of gridview. imageview custom background seems need. have check in getview in row in order know background color use.
Comments
Post a Comment