Android TableLayout 标题行

2024-06-28

好的,我已经有了这个 TableLayout,它充满了数据 - 所有行都是以编程方式添加的。我在 Horizo​​ntalScrollView 内部有 TableLayout,而后者又在 ScrollView 内部 - 这使我可以水平和垂直滚动。我现在想做的是向其中添加一个不会滚动的标题行。我尝试移动一些东西,以便我的两个滚动视图实际上都在 TableLayout 内部,并将 TableRows 添加到 Horizo​​ntalScrollView 中;我的希望是能够在滚动视图之外添加标题行。

我唯一能想到的另一件事是为标题行设置第二个表格布局,但让列对齐似乎很困难。有任何想法吗?


一种方法是将 TableLayout 嵌入另一个 TableLayout 的行中,并将标题放在前一行中,如下所示。对齐数据和标题需要将标题 View 对象和数据的 View 对象的layout_width属性设置为相同的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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android TableLayout 标题行 的相关文章

随机推荐