How to make the text direction from right to left

2019-01-10 19:05发布

问题:

I want to write text in (right to left language i.e. Arabic) in a TextView. But I want to make the text writing direction from right to left. gravity:rightwill align the text to right only. I want to justify the text from right to left ( to make the words and numbers appear in he entered order in the line ) . how ?

回答1:

Another clever way which can be used in older versions of android and can be used anywhere in string so it's handier even in latest version of android, is to include the right-to-left mark or even left-to-right mark in the string when it's needed:

left-to-right mark: ‎ or ‎ (U+200E)
right-to-left mark: ‏ or ‏ (U+200F)

This is how it's down:

String rtl = "Hello \u200F(سلام)";

The good thing about this method is you can even use it after punctuation like (,{,[,! which are positioned right incorrectly! examples:

سلام! // wrong position
سلام!‏ // right position by adding RLM after !

look how ! is positioned correctly in second line, actually there is an RLM after it, and you can copy paste it even if it's invisible.

The second good thing as you saw it is that you can use this method (these char codes) in any system that supports RTL like browsers, word and so on.



回答2:

Just put this property in your TextView and everything will workout just fine it will automatically detect your text and change accordingly

android:textDirection="anyRtl"


回答3:

Too late , but the android:gravity="right" worked.



回答4:

set this line in xml for textview :

 android:textDirection="locale"


回答5:

Try using

 textview.setTextDirection(View.TEXT_DIRECTION_RTL);

or

 textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);


回答6:

In case of normal edit text this will work

      android:textDirection="rtl"

But in case of password fields this is not enough then follow this;

     android:textDirection="rtl"
     android:gravity="right"


回答7:

try this as it worked for me android:textDirection="rtl"



回答8:

Add these to your editText:

android:gravity="right"
android:textDirection="rtl"

PS: android:textDirection requires API level 17



回答9:

just adding my own experience with such issues. in my case in addition to suggested solutions i also had to replace English " character with the RTL (in my case hebrew) representation of such character ״



回答10:

By using attributes as below in my xml code strangely I got a right to left text with correct punctuation. But this is not a perfect solution for multi language text:

android:textDirection="ltr"
android:gravity="right"

setting ltr corrects punctuation problems like having dot on right side instead of left.

gravity attribute makes text flow from right to left.



回答11:

best way textDirection for TextView, Don't use layoutDirection

  <TextView
     android:id="@+id/viewCountTv"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textDirection="anyRtl"
     android:gravity="center"/>


回答12:

Use android:layoutDirection="rtl" and android:textAlignment="viewStart" to make the text view right-to-left.