如何在React Native中为Android设置Component RTL

时间:2019-07-19 07:21:16

标签: react-native

我正在使用react-native构建移动应用,并且我想在react-native中为Android应用设置RTL。

我知道这是用于设置它的link,但是,我听不懂以下部分。

Making an App RTL-ready
To support RTL, you should first add the RTL language bundles to your app.

See the general guides from iOS and Android.
Allow RTL layout for your app by calling the allowRTL() function at the beginning of native code. We provided this utility to only apply to an RTL layout when your app is ready. Here is an example:

iOS:

// in AppDelegate.m
     [[RCTI18nUtil sharedInstance] allowRTL:YES];
Android:

// in MainActivity.java
     I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
     sharedI18nUtilInstance.allowRTL(context, true);

尤其是,我听不懂Android部分。如何将以下代码添加到MainActivity.java

     I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
     sharedI18nUtilInstance.allowRTL(context, true);

这是我的MainActivity.java


import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "VtuberDayoneApp";
    }
    I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
    sharedI18nUtilInstance.allowRTL(context, true);

}

这是在构建应用程序时显示的错误消息。

> Task :app:compileDebugJavaWithJavac FAILED
/Users/xxxx/Documents/projects/company/projectnam/HelloWorld/android/app/src/main/java/com/helloworld/MainActivity.java:16: error: <identifier> expected
    sharedI18nUtilInstance.allowRTL(context, true);

2 个答案:

答案 0 :(得分:0)

使用onCreate函数。

import com.facebook.react.modules.i18nmanager.I18nUtil;
...
    @Override
    public void onCreate() {
        super.onCreate();
        I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance(); 
        sharedI18nUtilInstance.allowRTL(context, true);
    }

现在请重新启用本机反应。此配置将需要重新启动开发过程。

答案 1 :(得分:0)

您必须像这样更改代码文件MainActivity.java: enter image description here 希望这可以帮助您!