0

I have this as part of my layout:

<LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:orientation="horizontal" android:layout_weight="0.15"> <TextView android:id="@+id/question_text" android:layout_width="0dip" android:layout_height="fill_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/score_label" /> <TextView android:id="@+id/score_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" /> </LinearLayout> 

The first TextView is empty at the beginning of the application. Its content is changed dynamically. This makes it occupy zero space so that the second TextView is aligned to the left, even though its layout_gravity is set to right.

How can I make it occupy a fixed width, without taking the contents into account?
I thought about using layout_weight, but I know the recommendation is against using nested weights (the parent ViewGroup has a layout_weight attribute). Maybe I should use a RelativeLayout?

Thanks for any suggestions.

1
  • You could add an image of the layout (a screenshot or a sketch is sufficient) to your post. This would help others to understand the layout much faster. Commented Sep 14, 2012 at 19:40

2 Answers 2

3

I solved a similar problem using the attribute android:ems="<some number>" on the TextView. An "ems" is the width of the character "M". This attribute makes the TextView exactly the given no. of ems wide. http://developer.android.com/reference/android/widget/TextView.html

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I'll check it, but if anyone else has another idea, they are welcome to answer.
1

You have all of your TextViews width set to android:layout_width="wrap_content" which means that if there's nothing in there it will have no width. You need to set that to either "match_parent" which will make it the same width as it's parent container or set it to a fixed value, something like android:layout_width="100dp".

2 Comments

Actually the first TextView's width is 0dip, which is equivalent to fill/match_parent.
It's only equivalent to fill_parent / match_parent if you also set the layout_weight to 1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.