将 TextView 添加到“可堆叠”的 TableLayout

发布于 2024-09-12 11:31:52 字数 1082 浏览 2 评论 0原文

好的,我的问题是,我有 n-TextView,它们将以编程方式添加到 TableLayout 中。我如何设置这些 TextView 或 TableLayout 的样式完全不重要,每次我添加一些内容时,它都会在底部添加其他 TextView 之后的 TextView。 这些视图有一个可变的宽度,它是根据它们的文本长度和一些像素计算出来的(WRAP_CONTENT 只是设置了 100% 宽度...)。

It is like this right now:
------------------------------------------
[TextView 1]
[TextView 2]
[TextView 3]
[TextView 4]
------------------------------------------

And it should be like:
------------------------------------------
[TextView 1] [TextView 2] [TextView 3]
[TextView 4]
------------------------------------------

实际上我不需要 TableLayout。我仍然可以将其更改为您想要的任何内容。当然如果这些TextView能够获取WRAP_CONTENT作为宽度就更好了。

编辑: 顺便提一句。带有orientation =“horizo​​ntal”的LinearLayout首先添加TextViews,但最后它不会换行到下一行,它只是将它们添加到右侧并且它将被分割,例如:

------------------------------------------
[TextView 1] [TextView 2] [TextView 3] [Te
                                       [xt
                                       Vie
                                       w 4
                                       ]
------------------------------------------

Ok, my problem is, I have got n-TextViews and they will be added programmatically into a TableLayout. It totally doesnt matter, how I style these TextViews or the TableLayout, everytime I add something, it adds the TextView on the bottom after the other TextViews.
These Views have got a variable width, which is calculated out of their textlength and some pixels (WRAP_CONTENT just made 100% width...).

It is like this right now:
------------------------------------------
[TextView 1]
[TextView 2]
[TextView 3]
[TextView 4]
------------------------------------------

And it should be like:
------------------------------------------
[TextView 1] [TextView 2] [TextView 3]
[TextView 4]
------------------------------------------

Actually I dont need a TableLayout. I still can change it in whatever you want. Of course it would be even better if those TextViews can get WRAP_CONTENT as width.

EDIT:
Btw. a LinearLayout with orientation="horizontal" adds the TextViews at first corret, but on the end it doesn't wrap to the next line, it just adds them to the right side and it will be splitted, like:

------------------------------------------
[TextView 1] [TextView 2] [TextView 3] [Te
                                       [xt
                                       Vie
                                       w 4
                                       ]
------------------------------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-09-19 11:31:52

我想你可以尝试这样的事情:
- 将 TableLayout 作为 Activity 的根,并以 FILL_PARENT 作为宽度和高度。
- 然后获取其宽度并将其存储在变量(mWidth)中。
- 创建一个 TableRow,添加第一个 TextView 并将其宽度存储在变量 (mTotalWidth) 中,然后将 TextView 放入行中.
- 当您想要添加新的 Textview 时,您可以创建它,获取其宽度,并在将其添加到 mTotalWidth 时计算总和。
现在
如果 mTotalWidth < mWidth ,您将 TextView 添加到当前行并增加 mTotalWidth
else您创建一个新的TableRow,将您的Textview放入其中,并将mTotalWidth的值更改为此TextView<的宽度/代码>。
然后您可以对所有视图重复此操作。这可能看起来有点混乱,但我认为它可以工作。

I think you could try something like that :
- Put a TableLayout as root of your Activity, with FILL_PARENT as width and height.
- Then get its width and store it in a variable (mWidth).
- you create a TableRow, add a first TextView and store its width in a variable (mTotalWidth), and you put the TextView in the row.
- When you want to add a new Textview, you create it, get its width and calculate the sum when you add it to mTotalWidth.
Now
if mTotalWidth < mWidth , you add your TextView into the current row and increment mTotalWidth
else you create a new TableRow, put your Textview in it, and change the value of mTotalWidth to the width of this TextView.
Then you can repeat this for all your views. This can appear a bit messy but I think it could work.

呢古 2024-09-19 11:31:52

您可能想要使用 GridView为了你想做的事。我不相信有任何布局类支持您想要做的事情。

如果您使用 GridView ,您应该能够使用 ArrayAdapter 因为它已经支持轻松创建 TextView

如果您只是想要一个在下一个子项不适合当前行时环绕的布局,那么您将必须编写一个自定义布局类来完成它。

You might want to use a GridView for what you are trying to do. I do not believe there are any Layout classes that support what you want to do.

If you go with GridView you should then be able to use the ArrayAdapter since it already supports creating TextViews easily.

If you just want a Layout that wraps around if the next child does not fit in the current row then you're going to have to write a custom layout class to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文