好的,所以我有了这个TableLayout及其全部数据-并以编程方式添加了所有行。我将TableLayout放在HorizontalScrollView的内部,而它又在ScrollView的内部-这使我可以水平和垂直滚动。我现在想做的是向其中添加一个不会滚动的标题行。我尝试过四处移动,以使两个滚动 View 实际上都位于TableLayout内,并将TableRows添加到HorizontalScrollView中。我希望能够然后在滚动 View 之外添加标题行。
我唯一能想到的就是为标题行设置第二种表布局,但是要使列对齐似乎很困难。有任何想法吗?
最佳答案
一种方法是将TableLayout嵌入另一个TableLayout的行中,并将标题放在前一行中,如下所示。对齐数据和 header 需要将 header View对象的layout_width属性和数据的View对象设置为相同的下垂值。另外,内部TableLayout的View对象的layout_weight属性必须与其对应的标题匹配。
现在,在下面的XML中,我在内部TableLayout中将3个TextViews放在一行中,以与列标题匹配。这只是为了说明如何进行对齐。您可以通过扩大布局并在运行时添加它来以编程方式填充该数据。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Name"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1"/>
<TextView android:text="Score"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="Level"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
<ScrollView android:layout_height="120dp">
<TableLayout android:id="@+id/score_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Al"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1">
</TextView>
<TextView android:text="1000"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="2"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</TableLayout>