创建具有固定标题行的TableView(不滚动)

She*_*tib 6 android

我有一个名为aTable的TableView.initialiseDTable()将第一行添加为RED背景

目标

  1. 防止标题行滚动(WORKING FINE)

  2. 保持标题行与每个TextView的宽度相同,这样可以感觉它是aTable(不工作)的子节点

学尝试

我的尝试是在scrollview之前创建一个tableview并填充aTable,然后提取标题行并将其作为此tableview的子项插入.

将代码添加到表格中的代码

    public void initialiseDTable() {
    LayoutInflater inflater = getLayoutInflater();
    TableRow row = (TableRow) inflater.inflate(R.layout.table_row, dTable,
            false);
    View v = (View) inflater.inflate(R.layout.table_cell, row, false);
    TextView A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag1);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag2);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag3);
    row.addView(v);
    v = (View) inflater.inflate(R.layout.table_cell, row, false);
    A = (TextView) v.findViewById(R.id.table_cell_text);
    A.setText(tag4);
    row.addView(v);
    if (this.showEST) {
        v = (View) inflater.inflate(R.layout.table_cell, row, false);
        A = (TextView) v.findViewById(R.id.table_cell_text);
        A.setBackgroundColor(Color.RED);
        A.setText(tag5);
        row.addView(v);
    }
    dTable.addView(row);
}
Run Code Online (Sandbox Code Playgroud)

public void updateATable() {
    aTable.removeAllViews();
    initialiseATable();
    for (int i = 0; i < newA.size(); i++)
        aTable.addView(newA.get(i)); //newA is an ArrayList <TableRow>
    View view = aTable.getChildAt(0);
    aTable.removeViewAt(0);
    aTableH.removeAllViews();
    aTableH.addView(view,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
Run Code Online (Sandbox Code Playgroud)

XML

        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:gravity="center" android:id="@+id/aLL">
            <TableLayout android:shrinkColumns="5"
                android:stretchColumns="1,2,3,4" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:id="@+id/aTableH">
            </TableLayout>
            <ScrollView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/aTableSV">
                <TableLayout android:shrinkColumns="5"
                    android:stretchColumns="1,2,3,4" android:layout_height="fill_parent"
                    android:layout_width="fill_parent" android:id="@+id/aTable">
                </TableLayout>
            </ScrollView>
        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Mar*_*son 5

我正在撰写一系列博客文章,涵盖了这个精确的问题.第一个是在这里,并描述了已经给出的答案的类似方法.然而,接下来的两篇文章涵盖了更优雅的解决方案.