TextView
的默认文本方向(textDirection
)和布局方向(layoutDirection
)通常是基于应用的整体配置以及设备的区域设置(locale)自动决定的。textDirection
的默认值:TextView
的文本方向是由系统自动设置的。具体而言,它默认的方向是 TEXT_DIRECTION_FIRST_STRONG
。TextView
将根据文本内容的第一个强方向性字符(例如一个字母或一个数字)来决定文本方向。如果第一个强方向性字符属于一种从左到右(LTR)语言,那么文本的方向将是从左到右。如果第一个强方向性字符属于一种从右到左(RTL)语言,那么文本的方向将是从右到左。layoutDirection
的默认值:启用RTL支持:
<application>
标签中添加:
<application
...
android:supportsRtl="true">
...
</application>
2、切换到右语言,重新设置Context的Locale,重启App即可生效
一般右语言,TextView的默认行为都没问题,因为文案也是对应的右语言语种,但如果对应的文案没有翻译成右语言,比如是写死的中文,那TextView就不会按照右语言来处理了,这个时候就需要为TextView设置textDirection属性,有2种方式
直接布局里设置android:textDirection="locale"
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/dp_16"
android:ellipsize="end"
android:gravity="center_vertical|start"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="@dimen/sp_14"
tools:text="appnameappnameappne"
android:textDirection="locale"/>
或者代码里设置setTextDirection
if (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL) {
viewBinding.tvName.setTextDirection(View.TEXT_DIRECTION_RTL);
} else {
viewBinding.tvName.setTextDirection(View.TEXT_DIRECTION_LTR);
}
码字不易,求转发,求点在看,求关注,感谢!