Android - 对齐 TableLayout 的列
Android - Aligning columns of TableLayout
我正在尝试在具有固定 header 行的水平和垂直滚动 table 布局中显示数据。我 运行 遇到的问题是,每当我将新的 TableRow 添加到 ScrollView 内的 LinearLayout 时,列将不会与 header 行列对齐。但是,如果我将新的 TableRow 添加到 TableRow 的正下方 header,列将对齐。
<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 做这种事情,因为它没有公开它如何调整大小的机制,它都是包私有的东西。
幸运的是,我很快就会开源并发布基于 Table 的布局的第一个 alpha 版本,该布局具有固定的行和列 Headers,具有多方向“滚动”和缩放。
还有很多事情要做,大遗漏的项目是里面没有什么是可以点击的。
更新
发布于 https://github.com/Zardozz/FixedHeaderTableLayout
一个简短的动作动画
我正在尝试在具有固定 header 行的水平和垂直滚动 table 布局中显示数据。我 运行 遇到的问题是,每当我将新的 TableRow 添加到 ScrollView 内的 LinearLayout 时,列将不会与 header 行列对齐。但是,如果我将新的 TableRow 添加到 TableRow 的正下方 header,列将对齐。
<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 做这种事情,因为它没有公开它如何调整大小的机制,它都是包私有的东西。
幸运的是,我很快就会开源并发布基于 Table 的布局的第一个 alpha 版本,该布局具有固定的行和列 Headers,具有多方向“滚动”和缩放。
还有很多事情要做,大遗漏的项目是里面没有什么是可以点击的。
更新
发布于 https://github.com/Zardozz/FixedHeaderTableLayout
一个简短的动作动画