0

I have GridView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/frag_books_grid_view_layout"
     android:orientation="vertical">
        <GridView
            android:cacheColorHint="@color/bg_default"
            android:listSelector="@drawable/list_selector_background"
            android:id="@+id/frag_books_grid_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:columnWidth="120px"
            android:gravity="center"
            android:horizontalSpacing="15dp"
            android:scrollbars="none"
            android:stretchMode="columnWidth"
            />
</LinearLayout>

And single item (placed in cell of this grid):

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:cacheColorHint="@color/bg_default"
    android:scrollbars="none"
    android:listSelector="@drawable/list_selector_background"
    android:layout_gravity="center_vertical|left"
    android:gravity="center_vertical|left"
    android:padding="15dp"
    android:id="@+id/frag_book_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="5dp"/>

How it looks: https://i.stack.imgur.com/2q51E.png Everything is fine with blue selection, but the contents of the cell is not centered inside of it. This picture explains what I want: https://i.stack.imgur.com/6o8zh.png

alwx
  • 205
  • 1
  • 3
  • 14
  • Don't know if its causing your problem, but list view inside grid view is generally not recommended / will not work correctly, since both are scrollable. – User Sep 03 '12 at 12:42
  • ok, but if i place ListView inside LinearLayout, i have the same problem – alwx Sep 03 '12 at 13:05
  • and have you got any recommendations to create something like that (http://i.imgur.com/OumWu.png) without ListView? – alwx Sep 03 '12 at 13:10
  • Set the width of the listview to fixed size or place inside a layout with fixed size to have the center effect you want – Marcio Covre Sep 03 '12 at 13:11

1 Answers1

0

Besides it's not recommended to have nested scrollable components, you have the dimensions of list view set to match_parent. So it's not possible to center, since it's already occupying all available space.

If you want to center, you first have make the listView smaller (setting fixed size). And then use gravity="center" on the container. You also can apply a uniform padding to the container, this will also make the listView look centered.

User
  • 31,811
  • 40
  • 131
  • 232