2012-06-20 50 views
1

我想將背景圖像設置爲GridView的所有行。 我用這個代碼:Android中的GridView行背景圖像

GridView gv = (GridView) findViewById(R.id.grid); 

    ArrayAdapter<String> aa = new ArrayAdapter<String>(
      this, 
      R.layout.row, 
      items); 

    gv.setAdapter(aa); 
    gv.setOnItemClickListener(this); 

main.xml中:(I設置的GridView設計這裏)

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    > 
<TextView 
    android:id="@+id/selection" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="14pt" 
    android:textStyle="bold" 


    /> 
<GridView 
    android:id="@+id/grid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:verticalSpacing="65px" 
    android:horizontalSpacing="5px" 
    android:numColumns="auto_fit" 
    android:columnWidth="100px" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:fastScrollEnabled="true" 
    /> 

row.xml:(I設置網格視圖設計這裏的行)

<LinearLayout 
xmlns:android = "http://schemas.android.com/apk/res/android" 
android:layout_width = "fill_parent" 
android:layout_height = "wrap_content" 
android:orientation = "horizontal" 
android:background="@drawable/rowbg" 
> 


<TextView 
android:id = "@+id/label" 
android:layout_width = "wrap_content" 
android:layout_height = "wrap_content" 
android:textSize  = "40sp" /> 

當我運行項目時,它強制關閉程序。但是,當我如下更改第二排,它運行良好,當然啦,沒有任何設計行:

ArrayAdapter<String> aa = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      items); 

其中我的代碼部分應該改變?

感謝


用eclipse運行程序後,會出現一個dialoge: 「應用程序已經停止unexceptedly請再試一次。」 和在logcat中顯示以下消息: 「12月6日至21日:05:25.724:E/dalvikvm(305):無法打開堆棧跟蹤文件 '/data/anr/traces.txt':權限被拒絕」

我也嘗試在Galaxy S上運行應用程序,並且看到相同的錯誤。

回答

1

創建一個擴展ArrayAdapter的適配器類。

在這個類的getView方法中,膨脹行的佈局,訪問數據應該設置到的textview。

1

一個好的解決辦法是繼承的GridView如下所述:

http://erikw.eu/android-row-background-in-gridview/

和轉載如下:

public class MyGridView extends GridView { 

    private Bitmap background; 

    public MyGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     background = BitmapFactory.decodeResource(getResources(), R.drawable.bg); 
    } 


    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     int count = getChildCount(); 
     int top = count &gt; 0 ? getChildAt(0).getTop() : 0; 
     int backgroundWidth = background.getWidth(); 
     int backgroundHeight = background.getHeight(); 
     int width = getWidth(); 
     int height = getHeight(); 

     for (int y = top; y &lt; height; y += backgroundHeight){ 
      for (int x = 0; x &lt; width; x += backgroundWidth){ 
       canvas.drawBitmap(background, x, y, null); 
      } 
     } 

     super.dispatchDraw(canvas); 
    } 
} 

使用

<your.package.name.MyGridView 
    android:id="@+id/mygridview" 
    <!-- other GridView configuration parameters --> 
    /> 
+0

注意,鏈接只有答案氣餒,引用往往會隨着時間的推移變得陳舊。考慮在這裏添加一個獨立的簡介,保持鏈接作爲參考。 – kleopatra