Reputation: 3435
I have a LinearLayout that comprises some number of TextViews. I set
android:gravity="center_horizontal"
for the parent Layout and result looks as below (for N=2):
I want it look like
In other words, I want them to be aligned to the left. More precisely, my plan is to find the longest TextView and then align other TextViews to the left bound of that view. Can anyone explain me in what callback of my Activity can I do it? I tried
public void onCreate(Bundle savedInstanceState)
but
textView.getWidth()
(that I use to find the longest TextView) returns 0 for all textViews. The second question is: what method should I use to move a TextView "n" pixels left?
Upvotes: 0
Views: 809
Reputation: 663
Hmm, it looks like the easiest route to get to what you want is embedding the LinearLayout in another Layout instead of attempting to do it by hand.
Possible approach:
RelativeLayout with gravity:center
Linear Layout with gravity:left and wrap_content as size
TextView1, wrap_content
TextView2
...
This should do the trick!
BTW, if you try to do anything with layout, overriding onCreate will never work! The layout will only be defined afterwards, when the drawing framework completed one "onMeasure" and one "onLayout" cycle. A (hacky) way to ensure that, is simply postpone that code (e.g., use "View.postDelayed(..)". A better way would be to have a custom, invisible View that is added in the onCreate method. When its onDraw was called for the first time, you can be sure that at least the first layout cycle was completed. All Views should then return their actual size.
In terms of moving Views: Did you have a look at "View.setOffset"?
Upvotes: 1