-1

我想在我的应用程序中有一个自定义视图,它由一个 ImageView 和一个可选的覆盖 TextView 组成。我怎样才能结合这些来创建一个单一的视图?我需要一个视图,因为我计划使用该视图来使用项目填充 GridView 或 ListView。是使用复合控件的最佳方式吗?这是否适用于适配器和 GridView?

4

1 回答 1

1

您只需像任何其他布局一样创建它,然后在您的适配器中将该布局用于网格视图。

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);

自定义网格.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>

它在行动中:

在此处输入图像描述

于 2012-06-17T14:55:08.713 回答