在LTR设备上强制RTL

时间:2017-05-26 23:17:19

标签: android react-native

标题说明了一切。我找到了几种使用Facebook的I18nManager将应用程序的默认布局设置为RTL的方法,但它仅在第二次应用程序启动时才这样做。

代码示例:I18nManager.forceRTL(true)

我不想让用户能够更改语言,因为应用程序本身是阿拉伯语。我到处搜索,但都讨论了如何支持RTL,而不是实际使用它作为默认布局。

有没有办法通过I18nManager实现这一点,还是我必须对我的本机代码进行一些更改?

4 个答案:

答案 0 :(得分:8)

首先,在清单中添加RTL支持,如下所示:

<application
    android:name=".application.SampleApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
</application>

然后,在您的启动器活动中,在onCreate()中添加以下代码。

Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; 
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());

答案 1 :(得分:3)

将这行代码添加到所有活动的onCreate方法(超级和setContentView之前)的最顶层:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

并确保在清单中你的supportRtl等于True。

答案 2 :(得分:0)

MageNative的答案是正确的。唯一的事情就是像这样设置locale。您需要使用setLocale()方法,如下所示:

Locale locale = new Locale("fa_IR"); // This is for Persian (Iran)
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics());

您需要导入java.util.Localeandroid.content.res.Configuration

答案 3 :(得分:0)

您必须在两个地方执行此操作:

首先:在您的清单的应用程序标签中,像这样添加对 RTL 的支持:

<application
    ...
    android:supportsRtl="true"
    ..>
</application>

第二个:将其添加为活动的 onCreate() 方法的第一行:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

如果您有基础活动,您可以执行相同的操作,并将其应用于其他子活动。

相关问题