Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
API 24及更高版本中关于区域设置更改的Android RTL问题_Android_Android Layout_Localization_Locale_Android 7.0 Nougat - Fatal编程技术网

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

API 24及更高版本中关于区域设置更改的Android RTL问题,android,android-layout,localization,locale,android-7.0-nougat,Android,Android Layout,Localization,Locale,Android 7.0 Nougat,我试图在运行时更改应用程序的区域设置。它在低于API 24级的安道尔工作正常。但在API级别24或更高的情况下,布局方向不会根据区域设置而改变。 下面是在运行时更改区域设置的代码。我使用了LocaleHelper类,如下所示 public class LocaleHelper { private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"; public static Co

我试图在运行时更改应用程序的区域设置。它在低于API 24级的安道尔工作正常。但在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;
    }
}
下面是我在活动课中使用的代码

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();
    }
}
在我的应用程序类中,我添加了以下代码

@Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
    }
现在,当我将语言环境从英语改为乌尔都语时,语言会发生变化,但布局方向并没有如预期的那样改变。当我再次点击乌尔都语时,布局方向改变(第二次尝试)。下面是参考的屏幕截图


请帮助解决此问题

问题似乎是它在第一次更新时没有反映布局方向的更改。我通过覆盖
Activity
onAttachedToWindow
方法解决了这个问题,如下所示:

@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);
    }
}
在API 25上测试,工作正常。小心点,尽管目前我不确定这种方法是否有副作用。尽管如此,我认为这是你正在寻找的


我还将更新以反映更优雅的通用代码。

您的问题是否已通过给定的答案得到解决?对我来说,onAttachedToWindow事件不会在运行时触发活动娱乐。只需将此块放入baseActivity到onCreate中,它就解决了RTL方向的问题,谢谢@Gunhan我做了所有的事情。布局已经改变了。但是语言仍然是英语,下面24小时很好
package www.ourshopee.com.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.RequiresApi;
import android.util.Log;

import java.util.Locale;

public class LocaleHelper {

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

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

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

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

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


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

           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();
    }



//    private static void updateResources(Context context, String language) {
//
//
//
//
//
//        Locale locale = new Locale(language);
//        Locale.setDefault(locale);
//        Configuration config = context.getResources().getConfiguration();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            config.setLocale(locale);
//        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            context.createConfigurationContext(config);
//        }
//        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
//
//
//
//
//
//
//    }







    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Log.d("LocaleHelper", "language above 24: "+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);
        Log.d("LocaleHelper", "language below 24: "+language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale);
        }
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());

        return context;
    }

















}