“colspan”相当于什么?在 Android TableLayout 中?
我在 Android 中使用 TableLayout。现在,我有一个 TableRow,其中包含两个项目,在其下方,还有一个 TableRow,其中包含一个项目。它呈现如下:
-----------------------------
| Cell 1 | Cell 2 |
-----------------------------
| Cell 3 |
---------------
我想要做的是让单元格 3 拉伸到两个上面的单元格,所以它看起来像这样:
-----------------------------
| Cell 1 | Cell 2 |
-----------------------------
| Cell 3 |
-----------------------------
在 HTML 中,我会使用 COLSPAN.... 如何在 Android 中实现此功能?
I'm using a TableLayout in Android. Right now I have one TableRow with two items in it, and, below that, a TableRow with one item it it. It renders like this:
-----------------------------
| Cell 1 | Cell 2 |
-----------------------------
| Cell 3 |
---------------
What I want to do is make Cell 3 stretch across both upper cells, so it looks like this:
-----------------------------
| Cell 1 | Cell 2 |
-----------------------------
| Cell 3 |
-----------------------------
In HTML I'd use a COLSPAN.... how do I make this work in Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
似乎有一个属性可以做到这一点:
layout_span
更新:
此属性必须应用于 TableRow 的子级。不是 TableRow 本身。
It seems that there is an attribute doing that :
layout_span
UPDATE:
This attribute must be applied to the children of the TableRow. NOT to the TableRow itself.
为了完成答案,必须将layout_span属性添加到子级,而不是TableRow。
此代码片段显示了我的 tableLayout 的第三行,它跨越 2 列。
Just to complete the answer, the layout_span attribute must be added to the child, not to TableRow.
This snippet shows the third row of my tableLayout, which spans for 2 columns.
这就是您以编程方式执行此操作的方式
And this is how you do it programmatically
您必须使用layout_weight来填充整行,否则它仍然填充表布局的左列或右列。
You have to use layout_weight to fill the entire row otherwise it still fills left or right column of table layout.
也许这会对某人有所帮助。我尝试使用
layout_span
解决方案,但这对我不起作用。所以我用这个技巧解决了这个问题。只需在需要 colspan 的地方使用LinearLayout
代替TableRow
即可。Maybe this will help someone. I tried the solution with
layout_span
but this not working for me. So I solved the problem with this trick. Just useLinearLayout
in place ofTableRow
where you need colspan, that's all.在 TableRow 元素的子元素中使用 android:layout_span
use android:layout_span in child element of TableRow element
我在使用代码生成的 TableRow、Textview 等行跨度方面遇到了一些问题。即使 Onimush 的答案看起来不错,它也不适用于生成的 UI。
这是一段代码......不起作用:
代码似乎没问题,但是,当您到达“the_params”的 init 时,它返回 NULL。
另一方面,这段代码的工作方式就像一个魅力:
唯一的区别是我在设置跨度之前将 Textview 推入 TableRow 内。在这种情况下,它有效。
希望这会对某人有所帮助!
I've had some problem with rowspan, in case of TableRow, Textview and so on, generated with code. Even if Onimush answer seems to be good, it don't works with generated UI.
Here is a piece of code which.... don't work:
The code seems to be OK but, when you reach the init of "the_params" it returns NULL.
On the other end, this code works like a charm:
The only difference is that I push the Textview inside the TableRow before setting the span. And in this case, it works.
Hope this will help someone!
实际上它非常简单。这是我以编程方式解决的方案
Actually It is pretty straight forward. This is my solution programmatically
我认为你需要将一个布局包裹在另一个布局周围。
垂直有一个布局列表,内部有另一个(或在本例中为两个)水平列表。
我仍然发现在 Android 中很难将界面很好地分割为 50-50 部分。
I think you need to wrap a layout around another one.
Have one Layout list vertically, inside have another one (or in this case, two) list horizontally.
I'm still finding it hard to nicely split interface to 50-50 portion in Android.