Android - 对齐 TableLayout 的列

问题描述

我试图在具有固定标题行的水平和垂直滚动表格布局中显示数据。我遇到的问题是,每当我向 ScrollView 内的 LinearLayout 添加新的 TableRow 时,列将不会与标题行列对齐。但是,如果我直接在 TableRow 标题下方添加新的 TableRow,列将对齐。

<horizontalscrollview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECF0F1"
android:scrollbars="none"
android:overScrollMode="never">

 <TableLayout
    android:id="@+id/table_layout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:stretchColumns="*">

    <!-- Table Row Header -->
    <TableRow
        android:id="@+id/header_row"
        android:layout_width="fill_parent"
        android:background="@color/table_header_color">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lap"
            android:textSize="@dimen/table_header_text_size"
            android:textColor="@color/table_header_text_color"
            android:gravity="center"
            android:layout_marginLeft="12dp"
            >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/time"
            android:textSize="@dimen/table_header_text_size"
            android:textColor="@color/table_header_text_color"
            android:layout_marginLeft="@dimen/row_spacing"
            android:gravity="center"
            >
        </TextView>

        <TextView
            android:id="@+id/delta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/delta"
            android:textSize="@dimen/table_header_text_size"
            android:textColor="@color/table_header_text_color"
            android:layout_marginLeft="@dimen/row_spacing"
            android:gravity="center"
            android:layout_marginRight="12dp"
            >
        </TextView>
    </TableRow>

    <!-- Table Body -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:overScrollMode="never">

        <LinearLayout
            android:id="@+id/table_body"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">



        </LinearLayout>

    </ScrollView>

 </TableLayout>
</horizontalscrollview>

Screenshot of unaligned text

Screenshot of aligned text

解决方法

你不能用 TableLayout 做这种类型的事情,因为它没有公开它如何调整大小的机制,都是包私有的东西。

幸运的是,我很快就会开源并发布基于表格的布局的第一个 alpha 版本,该布局具有固定的行和列标题,具有多方向“滚动”和缩放功能。
还有很多事情要做,并且缺少的大项目还没有可点击的内容。

更新

https://github.com/Zardozz/FixedHeaderTableLayout 发布

它的一个简短动画

enter image description here