2012-09-03 59 views
0

我的GridView: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> 

而且單個項目(放置在這個格之格):

<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"/> 

如何看起來:http://i.imgur.com/rhZiu.png 一切都很好,與藍色選擇,但單元格的內容不在其中心。 這張照片說明了什麼我想:http://i.imgur.com/OumWu.png

+0

不知道它是否會導致你的問題,但一般不建議在網格視圖內的列表視圖/將無法正常工作,因爲兩者都是可滾動的。 – Ixx

+0

好的,但如果我把ListView放在LinearLayout中,我有同樣的問題 – alwx

+0

,並且你有沒有建立類似的東西(http://i.imgur.com/OumWu.png)沒有ListView? – alwx

回答

0

它除了不推薦嵌套滾動組件,所以必須設置爲match_parent列表視圖的尺寸。所以它不可能居中,因爲它已經佔據了所有可用的空間。

如果你想居中,你首先讓listView更小(設置固定大小)。然後在容器上使用gravity =「center」。您也可以將統一的填充應用於容器,這也會使listView看起來居中。