Android 如何使文本方向从右到左

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10964488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:35:36  来源:igfitidea点击:

How to make the text direction from right to left

androidtextviewright-to-left

提问by Adham

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 ?

我想在 TextView 中用(从右到左的语言,即阿拉伯语)编写文本。但我想让文字书写方向从右到左。gravity:right将仅将文本向右对齐。我想从右到左对齐文本(使单词和数字按他在行中输入的顺序出现)。如何 ?

回答by Ali

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 markor even left-to-right markin the string when it's needed:

可在旧版本的Android的使用,可以在字符串中使用任何地方,所以它的使用方便性,即使在Android的最新版本,是包含另一种巧妙的方式从右到左标记,甚至左至右符号的字符串需要时:

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.

看看怎么样正确定位在第二行,实际上它后面有一个RLM,即使它不可见,您也可以复制粘贴它。

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.

正如您所见,第二件好事是您可以在任何支持 RTL 的系统(如浏览器、word 等)中使用此方法(这些字符代码)。

回答by RaedMarji

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

只需将此属性放在您的 TextView 中,一切都会好起来的,它会自动检测您的文本并相应地更改

android:textDirection="anyRtl"

回答by losingsleeep

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

为时已晚,但android:gravity="right"奏效了。

回答by arshad shaikh

set this line in xmlfor textview :

xml 中为 textview设置这一行:

 android:textDirection="locale"

回答by manDroid

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"

回答by Iman Marashi

Try using

尝试使用

 textview.setTextDirection(View.TEXT_DIRECTION_RTL);

or

或者

 textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);

回答by Armin

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

使用android:layoutDirection="rtl"android:textAlignment="viewStart"使文本视图从右到左。

回答by user2876982

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

试试这个,因为它对我有用 android:textDirection="rtl"

回答by Kaaveh Mohamedi

Add these to your editText:

将这些添加到您的 editText:

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

PS: android:textDirectionrequires API level 17

PS:android:textDirection需要 API 级别 17

回答by Lord Tesla

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:

通过在我的 xml 代码中使用如下属性,我奇怪地得到了一个带有正确标点符号的从右到左的文本。但这不是多语言文本的完美解决方案:

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

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

设置 ltr 可以纠正标点符号问题,例如在右侧而不是左侧有点。

gravity attribute makes text flow from right to left.

重力属性使文本从右向左流动。