Adding TextViews to a TableLayout "stackable"
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 th开发者_运维问答e 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
]
------------------------------------------
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.
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 TextView
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.
精彩评论