第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

當(dāng)設(shè)備默認(rèn)語言不是英語時(shí)更改語言不起作用

當(dāng)設(shè)備默認(rèn)語言不是英語時(shí)更改語言不起作用

慕森卡 2021-12-18 09:58:16
當(dāng)我的設(shè)備語言不是英語(例如葡萄牙語)時(shí),我無法更改語言。這是我的代碼:  Locale locale = new Locale("en");        Locale.setDefault(locale);        Configuration config = new Configuration();        config.locale = locale;        context.getResources().updateConfiguration(config,                context.getResources().getDisplayMetrics());我檢查了其他一些像這樣的答案,但它也不起作用SharedPrefUtils.saveLocale(locale); // optional - Helper method to save the selected language to SharedPreferences in case you might need to attach to activity context (you will need to code this)Resources resources = getResources();Configuration configuration = resources.getConfiguration();DisplayMetrics displayMetrics = resources.getDisplayMetrics();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){    configuration.setLocale(locale);} else{    configuration.locale=locale;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){    getApplicationContext().createConfigurationContext(configuration);} else {    resources.updateConfiguration(configuration,displayMetrics);}那么我的問題是什么?
查看完整描述

3 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

您需要將新的配置上下文傳遞給 ContextWrapper 超類。


在您的活動(dòng)中覆蓋 attachBaseContext 并將新上下文傳遞為 -


@Override

protected void attachBaseContext(Context base) {

    super.attachBaseContext(updatedConfigurationContext(base));

}

并從 getApplicationContext().createConfigurationContext(configuration);


正如你在上面所做的那樣。


查看完整回答
反對(duì) 回復(fù) 2021-12-18
?
冉冉說

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

不知道你想在哪里做這個(gè),所以我只是假設(shè)它在一個(gè)活動(dòng)中。這個(gè)答案也在 Kotlin 中,如果你想在 Java 中使用它,請(qǐng)檢查以下帖子: How to convert a kotlin source file to a java source file


活動(dòng):


override fun attachBaseContext(ctx: Context?) {

    super.attachBaseContext(ContextWrapper.wrap(ctx, yourLocale))

}

上下文包裝器:


class ContextWrapper(context: Context?) : android.content.ContextWrapper(context) {

    companion object {

        fun wrap(context: Context?, locale: Locale): ContextWrapper {

            val configuration = context?.resources?.configuration

            configuration?.setLocale(locale)


            if (Build.VERSION.SDK_INT >= 24) {

                val localeList = LocaleList(locale)

                LocaleList.setDefault(localeList)

                configuration?.locales = localeList

            }


            val ctx = if(configuration != null) context.createConfigurationContext(configuration) else null

            return ContextWrapper(ctx)

        }

    }

}

重新創(chuàng)建您的上下文(活動(dòng)):


recreate()在您的活動(dòng)中使用以重新啟動(dòng)您的活動(dòng)上下文。


查看完整回答
反對(duì) 回復(fù) 2021-12-18
?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可能需要在更改默認(rèn)語言環(huán)境后重新創(chuàng)建活動(dòng)。

getActivity().recreate();


查看完整回答
反對(duì) 回復(fù) 2021-12-18
  • 3 回答
  • 0 關(guān)注
  • 181 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)