2011-05-03 58 views
29

即時通訊嘗試獲取圖像顯示在GridView中並獲取列自動設置,到目前爲止,我不得不手動設置不是我想要做的列數,因爲這會影響有多大圖像在不同大小的屏幕上。我試圖將其設置爲auto_fit,但它只在屏幕中間顯示2列。這是我嘗試實現:打開時爲橫向模式我想列自動調整,以便其所有的偶數(每個紅色方塊代表的圖像)GridView自動適合圖像

enter image description here

然後。 任何幫助將不勝感激,謝謝:)

回答

70

實驗與GridView屬性,特別是android:numColumns="auto_fit"android:stretchMode。對我來說,以下工作:

<GridView 
    android:id="@+id/myGrid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:columnWidth="60dp" 
    android:stretchMode="columnWidth"  
    android:gravity="center" 
/> 
+4

輝煌。我有auto_fit,但缺少columnWidth。我補充說,事情開始美麗的工作。 – 2011-09-14 02:22:25

+0

我除了columnWidth參數之外都有其他所有的東西,它必須有2列中的任何一個 – njzk2 2011-11-07 15:24:48

+2

我具有相同的確切XML,但GridView仍然只顯示2列! – 2013-06-13 18:57:49

13

我不知道爲什麼有人投票否決了我上面的答案(所以我再次投了票):我必須以編程方式解決類似的問題(使用ImageView兒童的GridView)。某些代碼爲清楚起見移除:

int iDisplayWidth = getResources().getDisplayMetrics().widthPixels ; 

    iImageWidth = iDisplayWidth 
     /iNumberOfColumns ; 
    gridview.setColumnWidth(iImageWidth); 
    gridview.setStretchMode(GridView.NO_STRETCH) ;  
    /* wrap_content in the xml file is supposed to do this, but it didn't seem to work */ 

最好的問候,

Piesia

+0

爲什麼NO_STRETCH在拉伸模式下比STRETCH_COLUMN_WIDTH好? – 2014-01-14 21:02:05