Đối với những người không hài lòng với việc phải xác định trước kích cỡ của bạn, tôi tìm thấy một chút của một hack đang làm việc cho tôi.
Về cơ bản, tạo một bảng riêng biệt cho tiêu đề và đặt nó lên bảng chính của bạn, nhưng với cùng một căn chỉnh trên cùng, sau đó tạo hai bản sao của hàng tiêu đề và sau khi thêm một bản vào bảng chính, thêm phần kia vào bảng tiêu đề và đặt bố cục của chế độ xem conParams thành hàng tạo bảng chính.
Đây là ví dụ cơ bản của tôi.
trong cách bố trí của bạn:
<HorizontalScrollView
android:id="@+id/table_horizontal_scroll_view"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:id="@+id/table_vertical_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid_table_layout"
/>
</ScrollView>
<TableLayout
android:layout_alignLeft="@+id/table_vertical_scroll_view"
android:layout_alignRight="@+id/table_vertical_scroll_view"
android:layout_alignStart="@+id/table_vertical_scroll_view"
android:layout_alignEnd="@+id/table_vertical_scroll_view"
android:layout_alignParentTop="true"
android:background="@color/grid_view_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/grid_floating_row_layout"
/>
</RelativeLayout>
</HorizontalScrollView>
Sau đó, khi bạn thêm hàng của bạn:
//clear out any views
tableLayout.removeAllViews();
floatingRowLayout.removeAllViews();
TableRow[] rows = getTableContentRows() // content of your table
TableRow[] titleRows = {getTitleRow(), getTitleRow()}; //two copies of your title row
tableLayout.addView(titleRows[0]); // first add the first title to the main table
addRows(rows) // add any other rows
floatingRowLayout.addView(titleRows[1]); // floatingRowLayout is connected to id/grid_floating_row_layout
titleRows[0].setVisibility(View.INVISIBLE); // make the title row added to the main table invisible
// Set the layoutParams of the two title rows equal to each other.
// Since this is done after the first is added to the main table, they should be the correct sizes.
for(int i = 0; i < titleRows[0].getChildCount(); i++) {
titleRows[1].getChildAt(i).setLayoutParams(titleRows[0].getChildAt(i).getLayoutParams());
}
thử này ans http://stackoverflow.com/questions/20241614/android-fixed-header-horizontal-and-vertical-scrolling-table/26211896#26211896 –
Không hỗ trợ cho cuộn ngang –