将 TextView 添加到“可堆叠”的 TableLayout
好的,我的问题是,我有 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 =“horizontal”的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可以尝试这样的事情:
- 将
TableLayout
作为Activity
的根,并以FILL_PARENT
作为宽度和高度。- 然后获取其宽度并将其存储在变量(mWidth)中。
- 创建一个
TableRow
,添加第一个TextView
并将其宽度存储在变量 (mTotalWidth) 中,然后将TextView
放入行中.- 当您想要添加新的
Textview
时,您可以创建它,获取其宽度,并在将其添加到 mTotalWidth 时计算总和。现在
如果
mTotalWidth < mWidth ,您将TextView
添加到当前行并增加 mTotalWidthelse
您创建一个新的TableRow
,将您的Textview
放入其中,并将mTotalWidth的值更改为此TextView<的宽度/代码>。
然后您可以对所有视图重复此操作。这可能看起来有点混乱,但我认为它可以工作。
I think you could try something like that :
- Put a
TableLayout
as root of yourActivity
, withFILL_PARENT
as width and height.- Then get its width and store it in a variable (mWidth).
- you create a
TableRow
, add a firstTextView
and store its width in a variable (mTotalWidth), and you put theTextView
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 yourTextView
into the current row and increment mTotalWidthelse
you create a newTableRow
, put yourTextview
in it, and change the value of mTotalWidth to the width of thisTextView
.Then you can repeat this for all your views. This can appear a bit messy but I think it could work.
您可能想要使用
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 theArrayAdapter
since it already supports creatingTextView
s 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.