Search code examples
androidgridviewnine-patch

Ninepatch Background shrinks display area of GridView


I've got a basic GridView that I've set up as follows:

<?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/MainMenuGridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:columnWidth="90dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
    />

Everything works fine until I add a ninepatch background. Once I add the background image like this:

 <?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainMenuGridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="@drawable/mainbackground"
/>

It breaks my gridview. Instead of displaying 3-4 columns (depending on screen resolution and orientation) all icons in the gridview display in a single column in the upper left corner. Furthermore, only two at a time are displayed and I have to scroll down to see the rest. I suspect that the gridview is being bound by the dimensions of the ninpatch's center logo, but I'm not sure.

My ninepatch has a small area in the middle with a logo that I want to be displayed without any skewing. The rest of the background is a small gradient that takes up the remainder of the screen. Any ideas on what's causing this?


Solution

  • Sounds like you have padding defined (the lines at the bottom and right of the ninepatch)? Try getting rid of those, so you only have content area defined (the lines at the top and left)