0

我创建了一个表格布局,其中标题是固定的,但其余列是可滚动的。

我是这样开始的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#255"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TableLayout
        android:id="@+id/table_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="1dp"
        android:layout_marginLeft="1dp"
        android:stretchColumns="*"
        android:background="@android:color/black" >

        <TableRow
            android:id="@+id/table_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/black" >


            <!-- android:layout_weight="1" -->
            <TextView
                android:textColor="@android:color/black"
                android:layout_margin="1dp"
                android:layout_weight="0"
                android:gravity="center_horizontal"
                android:background="#FFCCCCCC"
                android:text="Program" />

            <TextView
                android:textColor="@android:color/black"
                android:layout_margin="1dp"
                android:layout_weight="0"
                android:gravity="center_horizontal"
                android:background="#FFCCCCCC"
                android:text="Course #" />

            <TextView
                android:textColor="@android:color/black"
                android:layout_margin="1dp"
                android:layout_weight="0"
                android:gravity="center_horizontal"

                android:background="#FFCCCCCC"
                android:text="Book-Name" />
        </TableRow>

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:scrollbars="horizontal|vertical" >

            <TableLayout
                android:id="@+id/table_scrollable_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:stretchColumns="*"
                android:background="@android:color/black" >

                <TextView
                    android:textColor="@android:color/black"
                    android:layout_margin="0dp"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0"
                    android:gravity="center_horizontal"
                    android:background="#FFCCCCCC" />
            </TableLayout>
        </ScrollView>
    </TableLayout>

</RelativeLayout>

然后我以编程方式添加行和以编程方式添加列,使用:

TableRow row = new TableRow(this);


row.setId(++row_id);
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(1, 1, 1, 1);
row.setLayoutParams(params);

TextView column = new TextView(this);
column.setText(matcher.group(1));
column.setBackgroundColor(0xFFCCCCCC);
column.setGravity(Gravity.CENTER);
column.setLayoutParams(params);
row.addView(column);

.
.
.

layout.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

但是,添加项目后,它开始扭曲我的表,现在看起来像:

在此处输入图像描述 如何使标题和其他列对齐?如果我删除我的可滚动布局,它最终看起来正确:

在此处输入图像描述 但它不再是可滚动的了。任何想法如何让它看起来像上面但保持可滚动?

4

0 回答 0