當前位置: 首頁>>代碼示例>>Java>>正文


Java Configuration.setLocale方法代碼示例

本文整理匯總了Java中android.content.res.Configuration.setLocale方法的典型用法代碼示例。如果您正苦於以下問題:Java Configuration.setLocale方法的具體用法?Java Configuration.setLocale怎麽用?Java Configuration.setLocale使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.content.res.Configuration的用法示例。


在下文中一共展示了Configuration.setLocale方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: changeLocale

import android.content.res.Configuration; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static void changeLocale(String locale) {
    Context context = InstrumentationRegistry.getInstrumentation()
            .getTargetContext();

    Resources res = context.getApplicationContext().getResources();
    Configuration config = res.getConfiguration();


    config.setLocale(new Locale(locale));
    if (SDK_INT >= 25) {
        context.createConfigurationContext(config);
    } else {
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}
 
開發者ID:mozilla-mobile,項目名稱:firefox-tv,代碼行數:17,代碼來源:SwitchLocaleTest.java

示例2: detectLanguage

import android.content.res.Configuration; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static void detectLanguage(Context context) {
	SharedPreferences setting = PreferenceManager
			.getDefaultSharedPreferences(context);
	String language = setting.getString(Util.PREF_LANGUAGE, "auto");
	Resources res = context.getResources();
	Configuration conf = res.getConfiguration();
	switch (language) {
		case "en":
		case "zh":
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				conf.setLocale(new Locale(language));
			} else {
				conf.locale = new Locale(language);
			}
			break;
		default:
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				conf.setLocale(Resources.getSystem().getConfiguration().getLocales().get(0));
			} else {
				conf.locale = Resources.getSystem().getConfiguration().locale;
			}
	}
	DisplayMetrics dm = res.getDisplayMetrics();
	res.updateConfiguration(conf, dm);
}
 
開發者ID:collaction,項目名稱:content-farm-blocker-android,代碼行數:27,代碼來源:Util.java

示例3: wrap

import android.content.res.Configuration; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String lang = prefs.getString(Constants.PREF_APP_LANGUAGE, "");
    Locale locale = StringUtils.getLocale(lang);
    locale = (locale == null) ? Locale.getDefault() : locale;
    Configuration configuration = context.getResources().getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        Locale.setDefault(locale);
        configuration.setLocale(locale);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
        context = context.createConfigurationContext(configuration);
    } else {
        configuration.locale = locale;
        context.getResources().updateConfiguration(configuration, null);
    }

    return new InventumContextWrapper(context);
}
 
開發者ID:qqq3,項目名稱:inventum,代碼行數:27,代碼來源:InventumContextWrapper.java

示例4: onCreate

import android.content.res.Configuration; //導入方法依賴的package包/類
@Override
public void onCreate() {
    super.onCreate();
    Resources resources = getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    // 應用用戶選擇語言
    String language = SPUtil.getInstance(this).getLanguage();
    if ("".equals(language)){
        language =Locale.getDefault().getLanguage();
    }
    if (language.equals(SPUtil.LANGUAGE_CHINESE)) {
        config.setLocale(Locale.getDefault());
    } else  {
        config.setLocale(Locale.ENGLISH);
    }
    resources.updateConfiguration(config, dm);

    mContext=getApplicationContext();
}
 
開發者ID:EggUncle,項目名稱:XposedNavigationBar,代碼行數:21,代碼來源:MyApplication.java

示例5: setLocale

import android.content.res.Configuration; //導入方法依賴的package包/類
public static void setLocale(@NonNull Context context) {
    Locale locale = Preferences.get(context).getCurrentLocale();
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList.setDefault(new LocaleList(locale));
        configuration.setLocales(new LocaleList(locale));
        configuration.setLocale(locale);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    //Todo:
    // Find out a solution to use context.createConfigurationContext(configuration);
    // It breaks onConfigurationChanged()
    // Still can't find a way to fix that
    // No other options, better use deprecated code for now
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
 
開發者ID:danimahardhika,項目名稱:wallpaperboard,代碼行數:23,代碼來源:LocaleHelper.java

示例6: wrap

import android.content.res.Configuration; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String lang = prefs.getString(Constants.PREF_APP_LANGUAGE, "");
    Locale locale = StringUtils.getLocale(lang);
    locale = (locale == null) ? Locale.getDefault() : locale;
    Configuration configuration = context.getResources().getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        Locale.setDefault(locale);
        configuration.setLocale(locale);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
        context = context.createConfigurationContext(configuration);
    } else {
        configuration.locale = locale;
        context.getResources().updateConfiguration(configuration, null);
    }

    return new ShowChatContextWrapper(context);
}
 
開發者ID:ansarisufiyan777,項目名稱:Show_Chat,代碼行數:27,代碼來源:ShowChatContextWrapper.java

示例7: wrap

import android.content.res.Configuration; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, Locale newLocale)
{
    final Resources res = context.getResources();
    final Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);
    }
    else
    {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }
    return new ContextWrapper(context);
}
 
開發者ID:mkulesh,項目名稱:microMathematics,代碼行數:27,代碼來源:AppLocale.java

示例8: loadLocale

import android.content.res.Configuration; //導入方法依賴的package包/類
public static void loadLocale(Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Locale locale = new Locale(sharedPreferences.getString("language", "zh"));
    Locale.setDefault(locale);
    Configuration configuration = new Configuration();
    configuration.setLocale(locale);
    context.getResources().updateConfiguration(configuration, context.getResources().getDisplayMetrics());
}
 
開發者ID:alphater,項目名稱:garras,代碼行數:9,代碼來源:LocaleUtils.java

示例9: setConfiguration

import android.content.res.Configuration; //導入方法依賴的package包/類
/**
 * 設置語言
 */
public void setConfiguration() {
    Locale targetLocale = getLanguageLocale();
    Configuration configuration = mContext.getResources().getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(targetLocale);
    } else {
        configuration.locale = targetLocale;
    }
    Resources resources = mContext.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    resources.updateConfiguration(configuration, dm);//語言更換生效的代碼!
}
 
開發者ID:finddreams,項目名稱:AndroidMultiLanguage,代碼行數:16,代碼來源:MultiLanguageUtil.java

示例10: applyLanguageForContext

import android.content.res.Configuration; //導入方法依賴的package包/類
/**
 * Setting {@link Locale} for {@link Context}.
 */
@NonNull
public static Context applyLanguageForContext(@NonNull Context context, @NonNull Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return context;
    }
}
 
開發者ID:WeiXinqiao,項目名稱:Recognize-it,代碼行數:17,代碼來源:AlbumUtils.java

示例11: updateResources

import android.content.res.Configuration; //導入方法依賴的package包/類
private static void updateResources(Context context, String language) {
    Locale locale = getLocale(language);
    Locale.setDefault(locale);
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    context.createConfigurationContext(configuration);
}
 
開發者ID:ThirtyDegreesRay,項目名稱:OpenHub,代碼行數:8,代碼來源:AppUtils.java

示例12: updateResources

import android.content.res.Configuration; //導入方法依賴的package包/類
public static Context updateResources(Context context, Locale locale) {
    Locale.setDefault(locale);
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.setLayoutDirection(locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    } else {
        configuration.locale = locale;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return context;
    }
}
 
開發者ID:brevent,項目名稱:Brevent,代碼行數:15,代碼來源:LocaleUtils.java

示例13: updateLanguage

import android.content.res.Configuration; //導入方法依賴的package包/類
public static void updateLanguage(Activity a) {
    if (a == null)
        return;
    Context context = a.getApplicationContext();
    Locale l;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        l = context.getResources().getConfiguration().getLocales().get(0);
    else l = context.getResources().getConfiguration().locale;

    String languageSelection = DataUtils.getLanguage(context);
    if (l != null && l.getLanguage().equalsIgnoreCase(languageSelection))
        return;

    Configuration cfg = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        cfg.setLocale(new Locale(languageSelection));
    else cfg.locale = new Locale(languageSelection);
    context.getResources().updateConfiguration(cfg, null);

    // Restart activity and notifications
    a.finish();
    Intent intent = a.getIntent();
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    LessonNotificationScheduler.updateBroadcastReceiver(context);
    HomeworkNotificationScheduler.updateBroadcastReceiver(context);
}
 
開發者ID:gregoreesmaa,項目名稱:minu-poska-android,代碼行數:28,代碼來源:TextUtils.java

示例14: updateLanguageInContext

import android.content.res.Configuration; //導入方法依賴的package包/類
public static void updateLanguageInContext(Context context) {
    String languageSelection = DataUtils.getLanguage(context);
    Configuration cfg = new Configuration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        cfg.setLocale(new Locale(languageSelection));
    else cfg.locale = new Locale(languageSelection);
    context.getResources().updateConfiguration(cfg, null);
}
 
開發者ID:gregoreesmaa,項目名稱:minu-poska-android,代碼行數:9,代碼來源:TextUtils.java

示例15: updateResources

import android.content.res.Configuration; //導入方法依賴的package包/類
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);

    return context.createConfigurationContext(configuration);
}
 
開發者ID:jayakrishnan-pm,項目名稱:Change-Language-AtRuntime,代碼行數:11,代碼來源:LocaleHelper.java


注:本文中的android.content.res.Configuration.setLocale方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。