我希望在我的应用程序中有一个自定义视图,它包含一个ImageView和一个可选的叠加TextView。如何组合这些来创建单个视图?我需要一个视图,因为我打算使用视图用项目填充GridView或ListView。是使用compound controls的最佳方式,这是否适用于适配器和GridView?
答案 0 :(得分: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>
这就是行动: