我正在尝试实现一个包含使用TableLayout
显示的数据表(动态填充)的UI。我希望默认显示表的4列,能够水平滚动表以显示4个(或更多)其他列。这可以使用TableLayout
结合水平/垂直ScrollView
吗?我很欣赏任何展示这种风格的XML示例。
这是一个澄清的例子:
默认表格视图
向右滚动后查看
答案 0 :(得分:1)
您应该可以通过将TableLayout放在HorizontalScrollView中来实现此目的。在TableLayout中定义元素的宽度
像这样(我没有测试过):
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hsv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout android:layout_height="wrap_content"
android:gravity="center"
android:layout_width="wrap_content"
android:id="@+id/tl1"
android:scrollbars="horizontal">
// Your other elements go here.
</TableLayout>
</HorizontalScrollView>
答案 1 :(得分:0)
我不确定这是否可以直接解决您的问题,但请查看github上的android-viewflow。 https://github.com/pakerfeldt/android-viewflow
答案 2 :(得分:0)
我解决这个问题的方法是使用ViewFlipper
。我很惊讶以前没有人提过这个。
我改变了我的XML,看起来像这样:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hsv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ViewFlipper android:id="@+id/dataTableFlipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dataTable"
android:stretchColumns="*">
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dataTable2"
android:stretchColumns="*">
</TableLayout>
</ViewFlipper>
</ScrollView>
这样我可以将表的每个页面作为单独的TableLayouts
进行管理。
以编程方式声明ViewFlipper
允许我使用ViewFlipper.showNext()
在两个表之间切换。像魅力一样工作!