2012-09-11 72 views
0

我花了好幾個小時的時間,看了沒有解決方案時找到的每個相關的SO問題。Android GridView不能正確縮放

這裏是我的問題:

我有圖像的GridView控件。我需要3列(以匹配iOS版本和美學)。如果將numColumns設置爲3,則每行圖像行的頂部和底部之間會有很多空間。如果我將寬度設置爲自動填充,我總是得到2,他們看起來更好,但更喜歡3列。

我錯過了什麼?

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="top" > 

<LinearLayout 
    android:id="@+id/llayout"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    > 
    <ImageView 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#666666" 
     android:clickable="false" 
     android:contentDescription="@string/title_card_collection" 
     android:src="@drawable/sportscard2x" /> 
</LinearLayout> 

<GridView 
    android:id="@+id/cardGrid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_above="@+id/llayout" 
    android:gravity="center" 
    android:horizontalSpacing="2dip" 
    android:numColumns="3" 
    android:stretchMode="columnWidth" 
    android:layout_alignParentBottom="true" 
    android:verticalSpacing="2dip" > 
</GridView> 

Screenshot

我希望它是簡單的東西,我只是缺少。提前致謝。

更新:增加了截圖。問題是,只有1行顯示,如果向下滾動,您會看到其他3行,但中間有一個巨大的空間。

+2

參考這個答案http://stackoverflow.com/questions/12363106/gridview-height-is-too-tall-when-using-an-imageview-inside/12363218#12363218 –

+0

這並沒有幫助。已更新以顯示更多的XML – RiddlerDev

+0

您可以發佈截圖或想要的結果樣機嗎?你現在有什麼。我剛剛使用了三列的gridView,沒有任何問題 – Jin35

回答

3

<編輯>

請找 「提示3」 以下。代碼示例可以在here找到。

< /編輯>

我認爲這個問題是您設置<GridView>wrap_contentlayout_height。網格視圖似乎無法正確計算它的總高度,因此它只顯示一行。

您既可以設置layout_heightmatch_parent(或任何其他固定高度 - 像120dp或其他)或您可以儘量延長GridView Java對象,做一些自己的計算(那麼你就需要使用您的自定義網格查看您的XML佈局中的對象以及:<com.package.to.MyCustomGridView>)。

但是,我需要強調的是,在API 11之前,沒有任何明確的方法可以從Java代碼中的網格視圖中獲取列的數量(爲了計算你的數據適配器會產生很多行)。 getNumColumns在API 11中引入。沒有找到行間距的正確方法(在API 16中引入了getHorizontalSpacing()getVerticalSpacing()方法)。

供您參考:http://developer.android.com/reference/android/widget/GridView.html

提示1: 我沒有測試此我自己,但你可以嘗試這樣的事:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ... 
     /> 

    <GridView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1.0" 
     ... 
     /> 

</LinearLayout> 

即包括線性佈局中的網格視圖,並在佈置圖像視圖後向其添加所有可用空間。

提示2: 您可以編寫自己的列表視圖。索尼移動教程網站似乎有一些好吃的糖果(不,我不是索尼移動公司僱用的:-),不過;一個很好的教程是一個很好的教程):http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/

提示3: 你可以擴展Java GridView對象和重寫onMeasure方法如下面的例子。請記住,請參閱您的擴展GridView類Android的佈局XML文件中(如<com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... />

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int heightSpec; 

    // The great Android "hackatlon" (in order to enable "wrap_content" on grid views). 
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { 
     heightSpec = MeasureSpec.makeMeasureSpec(
       Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
    } 
    else { 
     heightSpec = heightMeasureSpec; 
    } 

    super.onMeasure(widthMeasureSpec, heightSpec); 
} 

希望你能找到一些靈感在這個:-)

乾杯, - DBM

+0

謝謝,我今晚會試試這個,告訴你它是怎麼回事 – RiddlerDev

+0

仍然不完美,但是提示3讓我走上了正確的道路,只需要現在就使用覆蓋。謝謝! – RiddlerDev

+0

太棒了!樂意提供幫助:-) – dbm