Android TableLayout 标题行
好的,我已经有了这个 TableLayout,它充满了数据 - 所有行都是以编程方式添加的。我在 HorizontalScrollView 内有 TableLayout,而它又在 ScrollView 内 - 这使我可以水平和垂直滚动。我现在想做的是向其中添加一个不会滚动的标题行。我尝试移动一些东西,以便我的两个滚动视图实际上都在 TableLayout 内部,并将 TableRows 添加到 HorizontalScrollView 中;我的希望是能够在滚动视图之外添加标题行。
我唯一能想到的另一件事是为标题行设置第二个表格布局,但让列对齐似乎很困难。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一种方法是将 TableLayout 嵌入另一个 TableLayout 的行中,并将标题放在前一行中,如下所示。对齐数据和标题需要将标题 View 对象和数据的 View 对象的layout_width属性设置为相同的dip值。此外,内部 TableLayout 的 View 对象的layout_weight 属性必须与其相应的标头匹配。
现在,在下面的 XML 中,我在内部 TableLayout 中的一行中放置了 3 个 TextView,以与列标题匹配。这只是为了展示如何完成对齐。您可以通过扩展布局并在运行时添加它来以编程方式填充该数据。
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.
我实际上想出了另一种不错的方法来做到这一点。
只需在垂直方向 LinearLayout 内正常构建表格,将标题行作为第一行。接下来,以编程方式删除第一行,然后将其作为第一个子行添加到 LinearLayout。这就像一个魅力。
编辑:这也无需指定静态列宽即可工作。
I actually came up with another decent way of doing this.
Simply build the table normally with the header row as the first row, inside of a vertical orientation LinearLayout. Next, programmatically remove the first row then add it as the first child to the LinearLayout. This worked like a charm.
Edit: This also works without having to specify static column widths.
我知道这个问题很老了,但这是谷歌给我的第一个问题,因为我也遇到了同样的问题。因为我认为我已经找到了更好的解决方案,所以我想分享它。
想法:将 TableLayout(在 ScrollView 内)放入relativelayout并创建一个覆盖层,它将在其他所有内容上绘制第一行(标题行)。
这是layout.xml:
这是代码:
I know that the question is old, but it was the first one, that Google gave me as I've had the same problem. And since I think I've found a better solution, I would like to share it.
Idea: put the TableLayout (inside the ScrollView) into RelativeLayout and create an overlay, that would draw the first (header) row over everything else.
Here is layout.xml:
And here is the code:
对于那些不喜欢预先定义尺寸的人,我发现了一些对我有用的技巧。
基本上,为标题制作一个单独的表并将其放在主表上,但具有相同的顶部对齐方式,然后创建标题行的两个副本,将一个添加到主表后,将另一个添加到标题表并设置子视图的layoutParams 到主表的行。
这是我的基本示例。
在您的布局中:
然后当您添加行时:
For those not happy with having to pre-define your sizes, I found a bit of a hack that's working for me.
Basically, make a separate table for the title and put it over your main table, but with the same Top alignment, then create two copies of the title row and after adding one to the main table, add the other to the title table and set the child view's layoutParams to the row form the main table.
Here's my basic example.
in your layout:
Then when you add your rows:
在 ScrollView 中使用两个 tableLayout 一个,例如
android:stretchColumns="*"
然后为 ROW 创建一个通用视图,最重要的是
android:layout_width="0dp"
现在在 Activity 中
Use Two tableLayout One in ScrollView like give
android:stretchColumns="*"
then create a common view for ROWs and most important mention
android:layout_width="0dp"
Now in Activity