API 24及更高版本中关于区域设置更改的Android RTL问题

man*_*ngh 3 android locale localization android-layout android-7.0-nougat

我试图在运行时更改应用程序的语言环境。在API级别24以下的Andorid中,它可以正常工作。但是在API级别24或更高版本中,布局方向不会根据语言环境而变化。下面是在运行时更改语言环境的代码。我使用了如下的LocaleHelper类

public class LocaleHelper {

    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    public static Context onAttach(Context context) {
        String lang = getPersistedData(context, Locale.getDefault().getLanguage());
        return setLocale(context, lang);
    }

    public static Context onAttach(Context context, String defaultLanguage) {
        String lang = getPersistedData(context, defaultLanguage);
        return setLocale(context, lang);
    }

    public static String getLanguage(Context context) {
        return getPersistedData(context, Locale.getDefault().getLanguage());
    }

    public static Context setLocale(Context context, String language) {
        persist(context, language);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResources(context, language);
        }

        return updateResourcesLegacy(context, language);
    }

    private static String getPersistedData(Context context, String defaultLanguage) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
    }

    private static void persist(Context context, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(SELECTED_LANGUAGE, language);
        editor.apply();
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Log.d("LocaleHelper", "language : "+language);
        Locale.setDefault(locale);

        Configuration configuration = context.getResources().getConfiguration();
        configuration.setLocale(locale);
        configuration.setLayoutDirection(locale);
        return context.createConfigurationContext(configuration);
    }

    @SuppressWarnings("deprecation")
    private static Context updateResourcesLegacy(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        configuration.setLayoutDirection(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是我在活动课程中使用的代码

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.titleTextView)
    TextView mTitleTextView;
    @BindView(R.id.descTextView)
    TextView mDescTextView;
    @BindView(R.id.aboutTextView)
    TextView mAboutTextView;
    @BindView(R.id.toTRButton)
    Button mToTRButton;
    @BindView(R.id.toENButton)
    Button mToENButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setTitle(getString(R.string.main_activity_toolbar_title));
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base));
    }

    @OnClick(R.id.toTRButton)
    public void onChangeToTRClicked() {
        updateViews("ur");
    }

    @OnClick(R.id.toENButton)
    public void onChangeToENClicked() {
        updateViews("en");
    }

    private void updateViews(String languageCode) {
        Context context = LocaleHelper.setLocale(this, languageCode);
        Resources resources = context.getResources();

        mTitleTextView.setText(resources.getString(R.string.main_activity_title));
        mDescTextView.setText(resources.getString(R.string.main_activity_desc));
        mAboutTextView.setText(resources.getString(R.string.main_activity_about));
        mToTRButton.setText(resources.getString(R.string.main_activity_to_tr_button));
        mToENButton.setText(resources.getString(R.string.main_activity_to_en_button));

        setTitle(resources.getString(R.string.main_activity_toolbar_title));
        this.recreate();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的应用程序类中,添加了以下代码

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
    }
Run Code Online (Sandbox Code Playgroud)

现在,当我将语言环境从英语更改为乌尔都语时,语言已更改,但布局方向未按预期更改。当我再次单击“乌尔都语”时,布局方向更改(第二次尝试)。以下是刷新的屏幕截图

在此处输入图片说明

应用程序语言已更改,但布局方向未更改

现在布局方向为RTL,但对于英语应为LTR

请协助解决问题

Gun*_*han 7

问题似乎在于它在第一次更新时没有反映出布局方向的变化。我通过重写下面的onAttachedToWindow方法解决了这个问题Activity

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        getWindow().getDecorView().setLayoutDirection(
                "ur".equals(LocaleHelper.getLanguage(this)) ?
                View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR);
    }
}
Run Code Online (Sandbox Code Playgroud)

经过API 25的测试,工作正常。请注意,尽管我目前尚不确定此方法的任何副作用。不过,我认为这是您要寻找的。

我还将更新博客文章,以反映出更优雅的通用代码。