Android TableLayout标题行

Jef*_*ger 13 android android-layout android-tablelayout

好的,所以我得到了这个TableLayout,以及它的全部数据 - 以编程方式添加了所有行.我在Horizo​​ntalScrollView中有TableLayout,而Horizo​​ntalScrollView又在ScrollView中 - 这使我可以水平和垂直滚动.我现在要做的是添加一个不会滚动的标题行.我试过移动东西,以便我的两个滚动视图实际上都在TableLayout中,并将TableRows添加到Horizo​​ntalScrollView; 我希望能够在滚动视图之外添加标题行.

我能想到的另一件事就是为标题行设置了第二个表格布局,但是让列排成一行似乎很难.有任何想法吗?

Win*_*Oak 15

一种方法是将TableLayout嵌入另一个TableLayout的行中,并将标题放在前一行中,如下所示.对齐数据和标题需要将标题View对象的layout_width属性和数据的View对象设置为相同的dip值.此外,内部TableLayout的View对象的layout_weight属性必须与其对应的标头匹配.现在,在下面的XML中,我在内部TableLayout中将3个TextView放在一行中以与列标题匹配.这只是为了说明如何进行对齐.您可以通过膨胀布局并在运行时添加它来以编程方式填充该数据.

<?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>
Run Code Online (Sandbox Code Playgroud)


khe*_*cks 13

我实际上提出了另一种体面的做法.

通常使用标题行作为第一行,在垂直方向LinearLayout内部构建表.接下来,以编程方式删除第一行,然后将其作为第一个子项添加到LinearLayout.这就像一个魅力.

编辑:这也可以工作,而无需指定静态列宽.