如何在以编程方式创建的 TableRow 之间添加分隔线?
我有一个在 Android 项目中以编程方式创建的 TableLayout。只要从数据库中获取更多行,我就会不断添加 TableRows。现在我想在 TableRow 之间添加分隔线,例如边框。
在我从 XML 静态创建的另一个 TableLayout 中,我使用 View 作为分隔符,使用 style.xml 进行样式设置。
我尝试将视图添加到表格布局中,如下所示:
View v=new View(this);
v.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
v.setBackgroundResource(R.drawable.rowseparator_shape);
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr);
tl.addView(v);
但它仅在收集所有 TableRows 之后添加一次。为每个添加的 tr 添加一个视图的明智方法是什么?或者我应该一起使用其他东西吗?
I have a TableLayout that is created programmatically in an Android project. I keep adding TableRows as long as there are more rows fetched from the database. Now I want to add separating lines, like a border, between the TableRows.
In my other TableLayout that I created statically from XML I used a View as a separator, style with a style.xml.
I tried adding a View to the tablelayout like so:
View v=new View(this);
v.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
v.setBackgroundResource(R.drawable.rowseparator_shape);
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr);
tl.addView(v);
But it only gets added once after all the collected TableRows. What would be a smart way of adding one View for each tr added? Or should I use something else alltogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,我创建了一个具有特定背景颜色的一像素高的视图。这对我有用。
Here I'm creating a view that is one pixel high with a specific background color. This works for me.
感谢 Madhusuthanan 的帮助。我花了一段时间寻找如何做到这一点,以简单地用水平线分隔 TextView。我以编程方式创建视图(不使用表布局)。这是我根据上述答案得出的结论:
简单!希望这对其他人有帮助!
Thanks to Madhusuthanan for this. I spent a while searching for how to do this to simply separate TextViews with a horizontal line. I was creating my view programmatically (without using a Table layout). Here is what I came up with based on the above answer:
Simple! Hope this helps someone else!
您可以使用 Listview,这会比这样做更容易、更好。
You can use Listview that will be easiler and better than doing this.