2013-07-10 54 views
12

以下是我的XML代碼:其中stretchMode在GridView中使用Android上

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridviewgallery" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="@dimen/gridview_column_width" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="0dp" 
    android:horizontalSpacing="0dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:background="#444444" 
/> 

當我使用Android:stretchMode =「columnWidth時」,它創造列之間的間隙(儘管它填滿整個屏幕寬度) 如果我使用Android:stretchMode =「無」,它就會被左對齊並沒有留在屏幕右側的間隙(雖然現在有柱之間沒有間隙),我想 問題是兩個:爲了填補屏幕寬度以及列之間沒有差距。 如何實現它?

+0

增加列數。 –

回答

5

這是非常簡單的傢伙,只是刪除線

android:columnWidth="@dimen/gridview_column_width" 

您可以指定任意數量的列,我給定列數= 4

android:numColumns = "4" 

這對我來說

工作正常
11

android:numColumns="auto_fit"android:stretchMode="columnWidth"android:columnWidth="60dp"組合會給你調整針對不同屏幕尺寸的佈局。比固定列數好得多。見下面的例子...

<GridView 
     android:id="@+id/tablegrid" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ffff7965" 
     android:horizontalSpacing="5dp" 
     android:verticalSpacing="5dp" 
     android:layout_marginStart="5dp" 
     android:layout_marginLeft="5dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
     android:columnWidth="60dp" 
     /> 
+0

我花了幾天的時間尋找這個解決方案!優秀! – Alexey

0

我知道這個問題是有點老了,但這裏是一個答案,我發現希望這可以幫助別人,只爲android:stretchMode使用spacingWidthUniform,因爲它在文檔here將伸展的距離說在列之間平等。

請參考上面的鏈接以獲取更多信息。