Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, if your <code>GridView</code> only has 2 rows and 2 columns and you don't plan to add more items, why don't you use instead a simple <code>RelativeLayout</code>(and a <code>FrameLayout</code> for each cell) to place those items in a grid like you want(or you could use a <code>TableLayout</code> with 2 rows)? It would be much easier to properly position them and the performance will be the same.</p> <p>If you still want to use the <code>GridView</code> then you would have to find the dimensions of the screen(like you already did) substract from that the space between the rows, the title bar height and the notification bar height and then divide this to your 2 rows. Then you would use this size that you previously calculated to set it as the height for the <code>LayoutParams</code> of the inflated item layout in the <code>getView</code> method of your adapter(don't forget the width).</p> <p>My advice would be to use another layout then the <code>GridView</code>. Example below(you could use an <code>include</code> to make it smaller)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;View android:id="@+id/anchor" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true" /&gt; &lt;RelativeLayout android:id="@+id/griditem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/anchor" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="3dp" android:layout_toLeftOf="@id/anchor" android:background="#ffffff" android:gravity="center" android:padding="5dp" &gt; &lt;ImageView android:id="@+id/grid_item_image1" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginRight="10dp" android:src="@drawable/shop_open" &gt; &lt;/ImageView&gt; &lt;TextView android:id="@+id/grid_item_label1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/grid_item_image" android:layout_alignRight="@id/grid_item_image" android:layout_below="@id/grid_item_image" android:layout_marginTop="5dp" android:gravity="center" android:text="@+id/label" android:textColor="#000000" android:textSize="15dp" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/griditem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/anchor" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_margin="3dp" android:layout_toRightOf="@id/anchor" android:background="#ffffff" android:gravity="center" android:padding="5dp" &gt; &lt;ImageView android:id="@+id/grid_item_image2" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginRight="10dp" android:src="@drawable/shop_open" &gt; &lt;/ImageView&gt; &lt;TextView android:id="@+id/grid_item_label2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/grid_item_image" android:layout_alignRight="@id/grid_item_image" android:layout_below="@id/grid_item_image" android:layout_marginTop="5dp" android:gravity="center" android:text="@+id/label" android:textColor="#000000" android:textSize="15dp" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/griditem3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/anchor" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:layout_margin="3dp" android:layout_toLeftOf="@id/anchor" android:background="#ffffff" android:gravity="center" android:padding="5dp" &gt; &lt;ImageView android:id="@+id/grid_item_image3" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginRight="10dp" android:src="@drawable/shop_open" &gt; &lt;/ImageView&gt; &lt;TextView android:id="@+id/grid_item_label3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/grid_item_image" android:layout_alignRight="@id/grid_item_image" android:layout_below="@id/grid_item_image" android:layout_marginTop="5dp" android:gravity="center" android:text="@+id/label" android:textColor="#000000" android:textSize="15dp" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:id="@+id/griditem4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/anchor" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_margin="3dp" android:layout_toRightOf="@id/anchor" android:background="#ffffff" android:gravity="center" android:padding="5dp" &gt; &lt;ImageView android:id="@+id/grid_item_image4" android:layout_width="60dp" android:layout_height="60dp" android:layout_marginRight="10dp" android:src="@drawable/shop_open" &gt; &lt;/ImageView&gt; &lt;TextView android:id="@+id/grid_item_label4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/grid_item_image" android:layout_alignRight="@id/grid_item_image" android:layout_below="@id/grid_item_image" android:layout_marginTop="5dp" android:gravity="center" android:text="@+id/label" android:textColor="#000000" android:textSize="15dp" &gt; &lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload