我正在尝试使用TableLayout在我的应用程序中创建基本数据表。第一行包含每列的标题。此后的每一行都是数据。我希望数据可滚动,同时保持第一个标题行可见(即第一行不会与其余行一起滚动)。
我尝试了类似以下的内容(失败):
<TableLayout>
<TableRow>
<!-- The headers -->
<TextView android:text="Header #1"/>
<TextView android:text="Header #2"/>
<TextView android:text="Header #3"/>
</TableRow>
<ScrollView>
<!--The rest of the data -->
<TableRow>
<TableRow>
...
</ScrollView>
</TableLayout>
这会导致程序崩溃。还有其他建议吗?
答案 0 :(得分:1)
崩溃是因为TableRow
只能TableLayout
作为直接后代。
您可能想要使用的是ListView
和其上方的“标题”。
示例(未完成):
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
>
<TextView
android:id="@+id/header1"
android:layout_weight="1"
/>
<TextView
android:id="@+id/header2"
android:layout_weight="1"
/>
<TextView
android:id="@+id/header3"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
...
/>
关键是layout_weight="1"
和android:layout_width="fill_parent"
将为您提供3个相同大小的视图。然后,您需要创建Adapter
并在Adapter
提供的自定义布局中为子视图执行相同的操作。您可能需要稍微调整填充/边距,以使它们水平排列。
答案 1 :(得分:1)
你不需要将tablerow作为直接后代,它会崩溃因为scrollview只接受一个孩子。在scrollview中放入另一个tablelayout并在其中放入行。
答案 2 :(得分:0)
在RelativeLayout
中使用ScrollView
和TableLayout
对齐到顶部,并使用相同的第一行填充TableLayouts
可以制作技巧。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:id="@+id/tabscrvie"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TableLayout>
</ScrollView>
<TableLayout
android:id="@+id/headergridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/tabscrvie"
>
</TableLayout>
</RelativeLayout>