android 区域设置,Android-定义默认区域设置

我不知道还有没有其他选择,一种方法是用编程的方式。根据

Ricardo's great solution

,我创建了以下类以更改默认本地:

public class LocaleHelper {

public static Context onAttach(Context context) {

String lang = Locale.getDefault().getLanguage();

if(lang.equals("it") || lang.equals("de"))

return context;

String locale = "de";

return setLocale(context, locale);

}

private static Context setLocale(Context context, String localeSpec) {

Locale locale;

if (localeSpec.equals("system")) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

locale = Resources.getSystem().getConfiguration().getLocales().get(0);

} else {

locale = Resources.getSystem().getConfiguration().locale;

}

} else {

locale = new Locale(localeSpec);

}

Locale.setDefault(locale);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

return updateResources(context, locale);

} else {

return updateResourcesLegacy(context, locale);

}

}

@TargetApi(Build.VERSION_CODES.N)

private static Context updateResources(Context context, Locale locale) {

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

configuration.setLocale(locale);

configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);

}

@SuppressWarnings("deprecation")

private static Context updateResourcesLegacy(Context context, Locale 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;

}

}

你应该在你的活动课上使用它:

@Override

protected void attachBaseContext(Context base) {

super.attachBaseContext(LocaleHelper.onAttach(base));

}

uk.co.chrisjenx.calligraphy

要更改字体,请执行以下操作:

@Override

protected void attachBaseContext(Context base) {

super.attachBaseContext(CalligraphyContextWrapper.wrap(LocaleHelper.onAttach(base)));

}

更新

post

我设法自动找到了提供的语言。所以你不用再硬编码了

it

de

或者当您向资源中添加新的翻译时提供的其他翻译。

public class LocaleHelper {

public static Context onAttach(Activity context) {

Set providedLangs = getProvidedLanguages(context);

String lang = Locale.getDefault().getLanguage();

if(providedLangs.contains(lang))

return context;

String locale = "de";

return setLocale(context, locale);

}

private static Set getProvidedLanguages(Activity activity){

DisplayMetrics metrics = new DisplayMetrics();

activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

Resources r = activity.getResources();

Configuration c = r.getConfiguration();

String[] loc = r.getAssets().getLocales();

Set providedLangs = new HashSet();

for (int i = 0; i < loc.length; i++) {

Timber.e("LOCALE: " + i + ": " + loc[i]);

c.locale = new Locale(loc[i]);

Resources res = new Resources(activity.getAssets(), metrics, c);

String s1 = res.getString(R.string.app_name);

c.locale = new Locale("");

Resources res2 = new Resources(activity.getAssets(), metrics, c);

String s2 = res2.getString(R.string.app_name);

if(!s1.equals(s2)){

if(!providedLangs.contains(loc[i]))

providedLangs.add(loc[i]);

}

}

return providedLangs;

}

private static Context setLocale(Context context, String localeSpec) {

Locale locale;

if (localeSpec.equals("system")) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

locale = Resources.getSystem().getConfiguration().getLocales().get(0);

} else {

locale = Resources.getSystem().getConfiguration().locale;

}

} else {

locale = new Locale(localeSpec);

}

Locale.setDefault(locale);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

return updateResources(context, locale);

} else {

return updateResourcesLegacy(context, locale);

}

}

@TargetApi(Build.VERSION_CODES.N)

private static Context updateResources(Context context, Locale locale) {

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

configuration.setLocale(locale);

configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);

}

@SuppressWarnings("deprecation")

private static Context updateResourcesLegacy(Context context, Locale 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值