2012-06-17 174 views

回答

1

您只需像創建其他任何佈局一樣創建它,然後在您的適配器中爲gridview使用該佈局。

Java代碼:

Cursor scheduleCursor = mDbHelper.fetchAllSchedules(); 
startManagingCursor(scheduleCursor); 

String[] from = new String[] { ScheduleDBAdapter.SCHEDULE_MACHINE, 
    ScheduleDBAdapter.SCHEDULE_PRIORITY, 
    ScheduleDBAdapter.SCHEDULE_RUNPART, 
    ScheduleDBAdapter.SCHEDULE_RUNJOB, 
    ScheduleDBAdapter.SCHEDULE_OPERATOR, 
    ScheduleDBAdapter.SCHEDULE_NXTJOB1, 
    ScheduleDBAdapter.SCHEDULE_NXTPRT1, 
    ScheduleDBAdapter.SCHEDULE_NXTJOB2, 
    ScheduleDBAdapter.SCHEDULE_NXTPRT2 }; 
int[] to = new int[] { R.id.txtMachine, R.id.txtPriority, 
    R.id.txtRunningPart, R.id.txtRunningJob, R.id.txtOperator, 
    R.id.txtNextJobNumber1, R.id.txtNextJobPart1, 
    R.id.txtNextJobNumber2, R.id.txtNextJobPart2 }; 
SimpleCursorAdapter schedule = new SimpleCursorAdapter(this, 
    R.layout.customgrid, scheduleCursor, from, to); 

GridView gridview = (GridView) findViewById(R.id.gridview); 
gridview.setAdapter(schedule); 

customgrid.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtMachine" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:gravity="left" 
      android:text="machine" /> 

     <TextView 
      android:id="@+id/txtPriority" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ff0000" 
      android:gravity="center" 
      android:text="priority" 
      android:textColor="#000000" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtRunningPart" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_span="2" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="part #" 
      android:textColor="#009900" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtRunningJob" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_span="2" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="job #" 
      android:textColor="#009900" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtOperator" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_span="2" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="operator" 
      android:textColor="#0000FF" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtNextJobPart1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="part #" 
      android:textColor="#FFFF00" /> 

     <TextView 
      android:id="@+id/txtNextJobNumber1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="job #" 
      android:textColor="#FFFF00" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow6" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <TextView 
      android:id="@+id/txtNextJobPart2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="part #" 
      android:textColor="#FFFF00" /> 

     <TextView 
      android:id="@+id/txtNextJobNumber2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#000000" 
      android:gravity="center" 
      android:text="job #" 
      android:textColor="#FFFF00" /> 
    </TableRow> 

</TableLayout> 

這裏,它是在行動:

enter image description here