在GridView中填充网格

时间:2012-09-03 11:49:43

标签: android xml android-layout android-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:padding="0dp"
            android:cacheColorHint="@color/bg_default"
            android:listSelector="@drawable/list_selector_background"
            android:id="@+id/frag_books_grid_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchMode="spacingWidth"
            android:scrollbars="none"/>
</LinearLayout>

单项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ListView
        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:paddingLeft="15dp"
        android:paddingRight="15dp" 
        android:id="@+id/frag_book_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="5dp"/>
</LinearLayout>    

问题是单个项目没有左右填充。选择项目时看起来很糟糕:http://i.imgur.com/rirNP.png

请帮助修复它。

2 个答案:

答案 0 :(得分:16)

试试吧

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

获取信息

http://developer.android.com/guide/topics/ui/layout/gridview.html

答案 1 :(得分:4)

这个最适合三列..

  <GridView
        android:id="@+id/gridview_Gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnWidth="65dp"
        android:gravity="center"
        android:horizontalSpacing="6dp"
        android:listSelector="#00000000"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="-1dp" />