Issue
Surely this is a frequent use-case but I can't seem to resolve it with a LinearLayout (or RelativeLayout) but maybe I'm missing something.
Best explained with an image:
I have a basic textview chatbubble with another textview aligned to the right (the timestamp for the message).
Both are wrap_content
(but the timestamp could be set to like precisely 40dp but doesn't seem to matter).
Now in first (top) message it is short so you can see the view to the right.
But in the second the string is long so the wrap_content
has taken up the whole width of the phone, pushing the timestamp textview out of the screen (horizontal linearlayout, but relativelative toRightOf
does the same)
So how can I place a timestamp aligned right of another view, but the timestamp will be guaranteed size (not wrap/squash -the message bubble string wraps instead)
(I realise I could use a RTL layout, but I have messages from both sides so I would need LTR in other cases + min sdk api 17 is too high for a layout hack).
Solution
If you use a LinearLayout
widget with horizontal orientation
and one of its children wants to take all the available horizontal space then you should use layout weight.
For the first TextView
set:
layout_width="0dp"
layout_weight="1"
For the second TextView
:
layout_width="wrap_content"
In this case you say that second TextView
will take as much horizontal space as needed and the first TextView
will take the rest. "0dp" is just an optimization to prevent extra layout measure calls.
Answered By - Gennadii Saprykin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.