Android TableLayout 标题行 [英] Android TableLayout Header row

查看:26
本文介绍了Android TableLayout 标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我得到了这个 TableLayout,它充满了数据——所有的行都是以编程方式添加的.我有一个 Horizo​​ntalScrollView 里面的 TableLayout,它又在一个 ScrollView 里面——这让我可以水平和垂直滚动.我现在要做的是向其中添加一个不会滚动的标题行.我试过移动一些东西,这样我的两个滚动视图实际上都在 TableLayout 内,并将 TableRows 添加到 Horizo​​ntalScrollView;我希望能够在滚动视图之外添加标题行.

Okay, so I've got this TableLayout, and its full of data - with all of the rows added programmatically. I've got the TableLayout inside of a HorizontalScrollView, which in turn is inside a ScrollView - this gives me scrolling both horizontally and vertically. What I'm trying to do now is add a header row to it that will not scroll. I've tried moving things around so that both of my scroll views were actually inside of the TableLayout and adding the TableRows to the HorizontalScrollView; my hope was to be able to then add the header row outside of the scroll views.

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

The only other thing I can think of is having a second table layout just for the header row, but getting the columns to line up seems like it would be difficult. Any ideas?

推荐答案

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

One approach is by embedding a TableLayout within another TableLayout's row and putting the header in the preceding row as seen below. Aligning the data and the header requires the layout_width property of the header View objects and the data's View objects to be set to the same dip values. Also, the layout_weight property of the inner TableLayout's View objects must match its corresponding header. Now, in the XML below, I have placed 3 TextViews in the inner TableLayout in a single row to match with the column headers. This is just to show how the alignment can be done. You can populate that data programmatically by inflating a layout and adding it at runtime.

<?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>

这篇关于Android TableLayout 标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆