天天看點

android中讓textview右對齊,在android TextView中右對齊文本

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 :)

android:layout_gravity is used to align the text view with respect to the parent layout.

android:gravity is used to align the text inside the text view.

Are you sure you are trying to align the text inside the text view to the right or do you want to move the text view itself to the right with respect to the parent layout or are you trying to acheive both

The following code snippet:

android:textAlignment="textEnd"

android:layout_gravity="end"

works great for me

Make sure that you are not using android:layout_width="wrap_content" if so then it won't be able to set the gravity because you are not having enough space for the text to align. Instead use android:layout_width="fill_parent"

android:gravity="right"

It is basically used in Linear Layout so you have to set your width of TextView or either EditText as

android:layout_width = "match_parent"

and

When you are working in Relative Layout, use

android:layout_alignParentRight="true"

In case somebody still needs

android:gravity="end"

If it is in RelativeLayout you can use android:layout_toRightOf="@id/"

Or

If you want to align to the right corner of the device the place android:layout_alignParentRight="true"

Make the (LinearLayout) android:layout_width="match_parent" and the TextView's android:layout_gravity="right"

Let me explain gravity concept using WEB because a lot of developer are aware of it.

android_layout_gravity behaves float:left|right|top|bottom

Whereas android_gravity work as text_align:centre|left|right

In Android float is android_layout_gravity and text_align: is android_gravity.

And android_layout_gravity applies relative to parent. where as android_gravity applies to its child or inner content.

Note android_gravity only works when its View is match_parent(when it have space to center).

Below works for me:

android:id="@+id/tv_username"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Sarah" />

android:id="@+id/tv_distance"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/tv_username"

android:layout_alignParentEnd="true"

android:layout_toEndOf="@+id/tv_username"

android:text="San Marco"

android:textAlignment="viewEnd" />

Please note the android:layout_alignParentEnd and android:textAlignment settings of the second TextView.

You can use:

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

this should serve the purpose

android:textAlignment="textStart"

I also faced the same problem and figured the problem was happening as the layout_width of the TextView was having wrap_content. You need to have layout_width as match_parent and the android:gravity = "gravity"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/tct_postpone_hour"

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:layout_gravity="center"

android:gravity="center_vertical|center_horizontal"

android:text="1"

android:textSize="24dp" />

makes number one align in center both horizontal and vertical.