Text align right android

Выравнивание текста справа в Android TextView

у меня есть TextView в моем приложении. Я хочу выровнять текст справа. Я попытался добавить:

но это не работает для меня.

что я могу делать неправильно?

18 ответов

Я думаю, что вы делаете это: android:layout_width = «wrap_content»
Если это так, сделайте следующее: android:layout_width = «match_parent»

В общем, android:gravity=»right» отличается от android:layout_gravity=»right» .

первый влияет на положение самого текста в представлении, поэтому, если вы хотите, чтобы он был выровнен по правому краю, то layout_width= должно быть «fill_parent» или «match_parent» .

второй влияет на положение представления внутри его родителя, другими словами-выравнивание самого объекта (поле редактирования или текстовое представление) внутри родительского представления.

лучшим решением является тот, который является самым простым, и тот, который делает меньше изменений в вашем поведении кода.

что делать, если вы можете решить эту проблему только с 2 свойства TextView ?

вместо того, чтобы изменить свой LinearLayout свойства, которые, возможно, могут изменить поведение LinearLayout Чайлдс?

используя этот путь, вам не нужно изменить LinearLayout свойства и поведение, нужно только добавить два следующих свойства для вашей цели TextView :

что было бы лучше изменить только вашу цель, чтобы решить ваше решение, вместо того, чтобы иметь шанс вызвать другую проблему в будущем, изменяя ваш целевой отец? подумайте об этом 🙂

android:layout_gravity используется для выравнивания текстового представления относительно родительского макета. android:gravity используется для выравнивания текста внутри текстового представления.

Читайте также:  Все для android виджеты

вы уверены, что пытаетесь выровнять текст внутри текстового представления справа или хотите переместить само текстовое представление вправо по отношению к родительскому макету или пытаетесь достичь обоих

убедитесь, что вы не используете android:layout_width=»wrap_content» Если это так, то он не сможет установить гравитацию, потому что у вас недостаточно места для выравнивания текста. Вместо этого используйте android:layout_width=»fill_parent»

Источник

How to right-align text in an Android TextView or EditText?

I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding

but this doesn’t seem to work. any other suggestions please?

5 Answers 5

You should use android:gravity=»right» . layout_gravity is for the view (EditText) alignment against the container.

You may use android:layout_alignParentRight=»true» to align the EditText’s right edge to its parent’s right edge. Also, if you really want to use the layout_gravity or gravity attributes, check out this article that discusses the proper use of them.

You can jst set property for edit text in property window i.e. Gravity to right or by adding code of lin e in xml file of UI : android:gravity=»right»

layout_gravity means that you are specifying the layouts attribute, not the element inside. In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not elements) are the same..

use layout width and height as wrap content inside relative layout :

Источник

How to Align Text To the Right in Android TextView?

I need to right align my text in a TextView like this.

How to achieve this?

Below is my layout.xml file’s textview

2 Answers 2

You could not align text in TextView if you set with is wrap_content, please set fill_parent or match_parent or specified witdh.

Читайте также:  Star wars knights the old republic андроид

You’re using android:layout_width=»wrap_content» which means its not able to set the gravity because you don’t have enough space for the text to align. Try using this instead:

Not the answer you’re looking for? Browse other questions tagged android layout textview or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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:

But this doesn’t work for me.

What might I be doing wrong?

20 Answers 20

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?

Читайте также:  Star wars kotor android требования

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 :

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 TextView

У меня есть TextView в моем заявлении. Я хочу выровнять текст справа. Я попытался добавить:

Но это не работает для меня.

Что я могу делать не так?

Я думаю, что вы делаете это: android:layout_width = «wrap_content»
если это так, сделайте это: android:layout_width = «match_parent»

В общем, android:gravity=»right» отличается от android:layout_gravity=»right» .

Первый влияет на положение самого текста в представлении, поэтому, если вы хотите, чтобы он был выровнен по правому краю, то layout_width= должно быть либо «fill_parent» или «match_parent» .

Второй влияет на положение View внутри его родителя, другими словами — выравнивание самого объекта (поле редактирования или текстовое представление) внутри родительского представления.

Лучшее решение — это то, которое является самым простым, и которое делает меньше изменений в поведении вашего кода .

Что делать, если вы можете решить эту проблему только с 2 свойствами на вашем TextView ?

Вместо того, чтобы менять свои LinearLayout свойства, которые могут изменить поведение LinearLayout детей?

Используя этот способ, вам не нужно изменять LinearLayout свойства и поведение, вам нужно только добавить два следующих свойства к вашей цели TextView :

Что может быть лучше, если вы измените только свою цель, чтобы решить ваше решение, вместо того, чтобы иметь шанс вызвать другую проблему в будущем, изменяя вашего целевого отца? подумай об этом 🙂

Источник

Оцените статью