如何动态有效地应用 RTL
How to apply RTL dynamically and effectively
我正在动态地创建和添加线性布局中的 TextView,每当我应用 layoutAmount.setRotationY(180);
布局都会改变它的方向,这是正确的,但其中 TextView 的文字也会改变方向,这是错误的,例如,如果单词 sorry 变成了 yrros,我该如何正确应用 RTL
谢谢!
以下是制作动态 RTL 布局的最佳方法,
如果你使用 ConstraintLayout
不要使用left and right
改为使用 start and end
Android 将在您更改语言环境时动态更改布局
You can find my answer here about how to change the language of the app by locale
更新:
在约束布局中
使用约束作为
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView_about_back"
app:layout_constraintTop_toTopOf="parent"
此外,不要使用左边距或右边距
如果是线性布局,
use gravity start and end
示例代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_image"
tools:context=".activity.HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/about_al_ameen"
android:textColor="@color/colorWhite" />
</LinearLayout>
选择语言时添加;
LocaleHelper.setLocale(LAngSelect.this, "ar");
然后用sqlite或其他保存。
然后转到 MainActivity 的意图:
在 MainActivity 上保存语言并添加:
if (langs.equals("ar")) {
forceRTLIfSupported();
}
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
//HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then
//change if it is english then don't
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
setLayoutDirection 会根据语言设置布局方向
-确保使用布局重力开始和结束
public static void setLanguage(Context context, String language) {
String[] localeLang = language.split("_");
Locale locale;
if (localeLang.length > 1)
locale = new Locale(localeLang[0], localeLang[1]);
else
locale = new Locale(localeLang[0]);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale);
((Activity) context).getWindow().getDecorView().setLayoutDirection(config.getLayoutDirection());
}
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
我正在动态地创建和添加线性布局中的 TextView,每当我应用 layoutAmount.setRotationY(180);
布局都会改变它的方向,这是正确的,但其中 TextView 的文字也会改变方向,这是错误的,例如,如果单词 sorry 变成了 yrros,我该如何正确应用 RTL
谢谢!
以下是制作动态 RTL 布局的最佳方法,
如果你使用 ConstraintLayout
不要使用left and right
改为使用 start and end
Android 将在您更改语言环境时动态更改布局
You can find my answer here about how to change the language of the app by locale
更新:
在约束布局中 使用约束作为
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/imageView_about_back"
app:layout_constraintTop_toTopOf="parent"
此外,不要使用左边距或右边距
如果是线性布局,
use
gravity start and end
示例代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_image"
tools:context=".activity.HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:text="@string/about_al_ameen"
android:textColor="@color/colorWhite" />
</LinearLayout>
选择语言时添加;
LocaleHelper.setLocale(LAngSelect.this, "ar");
然后用sqlite或其他保存。
然后转到 MainActivity 的意图:
在 MainActivity 上保存语言并添加:
if (langs.equals("ar")) {
forceRTLIfSupported();
}
private void forceRTLIfSupported()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
//HERE CHECK CONDITION FOR YOUR LANGUAGE if it is AR then
//change if it is english then don't
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
setLayoutDirection 会根据语言设置布局方向
-确保使用布局重力开始和结束
public static void setLanguage(Context context, String language) {
String[] localeLang = language.split("_");
Locale locale;
if (localeLang.length > 1)
locale = new Locale(localeLang[0], localeLang[1]);
else
locale = new Locale(localeLang[0]);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale);
((Activity) context).getWindow().getDecorView().setLayoutDirection(config.getLayoutDirection());
}
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}