Right align text in android TextView

I have a TextView in my application. I want to align the text in it to the right. I tried adding:

android:gravity="right"

But this doesn't work for me.

What might I be doing wrong?


I think that you are doing this: android:layout_width = "wrap_content"
If this is the case, do this: android:layout_width = "match_parent"


In general, android:gravity="right" is different from android:layout_gravity="right" .

The first one affects the position of the text itself within the View, so if you want it to be right-aligned, then layout_width= should be either "fill_parent" or "match_parent" .

The second one affects the View's position inside its parent, in other words - aligning the object itself (edit box or text view) inside the parent view.


The better solution is the one that is the most simple, and the one that does less modification in your code behaviour .

What if you can solve this problem only with 2 Properties on your TextView ?

Instead of needing to change your LinearLayout Properties that maybe can alter the behaviour of LinearLayout childs?

Using this way, you do not need to change LinearLayout properties and behaviour, you only need to add the two following properties to your target TextView :

android:gravity="right"
android:textAlignment="gravity"

What would be better to change only your target to solve your solution instead of having a chance to cause another problem in the future, modifying your target father? think about it :)

链接地址: http://www.djcxy.com/p/31482.html

上一篇: 三元操作符?:vs if ... else

下一篇: 在Android TextView中对齐文本