2011-07-11 50 views
7

試圖讓高分名單,這是滾動的,看起來像這樣:GridView的不同列大小

foo   1000 
bar   876 
foobar  500 
foobarfoo 1 

我目前正在與一個GridView這樣做。我想將名稱列的寬度設置爲屏幕的60%,並將分數列的寬度設置爲40%。可能嗎?

目前我正在嘗試通過一個costum適配器。這裏是它的getview功能:

public View getView(int position, View convertView, ViewGroup parent) { 
    TextView tv; 
    if (convertView == null) { 
     tv = new TextView(context); 
     tv.setTextSize(25); 
     tv.setTextColor(Color.WHITE);    

     if (position % 2 == 0) 
     { 
      tv.setLayoutParams(new GridView.LayoutParams((width/10)*6, 50)); 
     } 
     else 
     { 
      tv.setLayoutParams(new GridView.LayoutParams((width/10)*4, 50)); 
     } 
    } 
    else { 
     tv = (TextView) convertView; 
    } 

    tv.setText(texts[position]); 
    return tv; 
} 

該佈局是由gridview和一個按鈕在底部構建。該XML文件是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top"> 
    <GridView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:id="@+id/grid" 
       android:numColumns="2" 
       android:columnWidth="0dp" 
       > 
    </GridView> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/backstr"></Button> 

</LinearLayout> 

所以問題再次:是否有可能設置GridView允許添加不同大小的列?如果是的話,那我的方法很好? (可能不會,因爲它不工作:))我只是錯過了什麼?

預先感謝您!

+0

你能嘗試設置列寬爲wrap_content代替0dp,然後看看是否可行? –

+0

這可惜不是列寬的有效值。但感謝評論! – Szellszi

+0

在這種情況下,您可以使用ListView而不是GridView,並且對於每一行,使用包含兩個文本視圖(LinearLayout優先)的佈局,並將其填充到適配器的getView方法中。 –

回答

1

工作就像一個魅力!非常感謝Abhinav,他提出了很好的建議。這裏是誰有同樣的問題給大家代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
    TextView tv1; 
    TextView tv2; 
    LinearLayout ll; 
    if (convertView == null) { 
     tv1 = new TextView(context); 
     tv1.setTextSize(25); 
     tv1.setTextColor(Color.WHITE); 
     tv1.setGravity(Gravity.LEFT); 
     tv1.setPadding(5, 5, 5, 5); 
     tv1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
                  LinearLayout.LayoutParams.FILL_PARENT, 
                  (float) 3.0)); 

     tv2 = new TextView(context); 
     tv2.setTextSize(25); 
     tv2.setTextColor(Color.WHITE); 
     tv2.setGravity(Gravity.RIGHT); 
     tv2.setPadding(5, 5, 5, 5); 
     tv2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
                   LinearLayout.LayoutParams.FILL_PARENT, 
                   (float) 4.0)); 

     ll = new LinearLayout(context); 
     ll.setOrientation(0); 
     ll.setPadding(5, 5, 5, 10); 

     tv1.setText(names[position]); 
     tv2.setText(scores[position]); 

     ll.addView(tv1); 
     ll.addView(tv2); 
    } 
    else { 
     ll = (LinearLayout) convertView; 
     tv1 = (TextView) ll.getChildAt(0); 
     tv2 = (TextView) ll.getChildAt(1); 

     tv1.setText(names[position]); 
     tv2.setText(scores[position]); 
    } 

    return ll; 
} 

和XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top"> 
    <ListView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:numColumns="2" 
       android:columnWidth="0dp" 
       android:id="@+id/list"> 
    </ListView> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/back"></Button> 

</LinearLayout> 

如果你認爲你可以提高這個代碼(因爲我在這個領域的新手)唐毫不猶豫地回覆!提前致謝!

+1

很好的解釋,但如果我有3 coloumns那麼如何實現這? – Randroid

5

我們必須爲一個項目執行此操作,並且不想使用除GridView之外的其他任何內容,因爲功能重複,但在一個GridView上,我們需要稍微不同的視圖配置(但仍使用適配器和所有其他好東西)。我們發現如果你覆蓋GridViewlayoutChildren函數,你可以做到這一點,雖然它的記錄很差。

我們的實現有一個特殊的視圖,並進行了檢查,如果新的佈局已經實現:

@Override 
protected void layoutChildren(){ 
    super.layoutChildren(); 
    if(!isSpecial || layoutAlreadySet) return; 
    layoutAlreadySet = true; 
    //Implement special layout.... 
} 
+0

嘿史蒂夫。我遇到了這個線程,因爲我正在爲我的問題尋找解決方案。我嘗試在GridView中顯示兩種不同類型的自定義視圖。 A型的寬度爲150dp,B型的寬度爲310dp。我總是希望細胞包裹它的內容。你的解決方案可能嗎? – JensJensen

+1

此解決方案僅用於使GridView執行與通常不同的操作,但仍然以來自適配器的信息提供。使用不同的佈局對象(例如LinearLayout)可能會更容易做到您想要做的事情。也就是說,我們確實使用這段代碼在GridView中實現了一個自定義大小的單元格,所以如果這是您需要做的事情類型,那麼這是一條您可以採用的路線。 – Steve