首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android 應用內切換語言:Locale 設置與 Configuration 更新完整方案

     編輯:程式設計綜合問答

核心原理

修改應用級 Configuration.locale,並重建 Activity 使新語言生效。不影響系統語言。

基礎實現 (API 17-24)


public void setAppLanguage(Context context, String languageCode) {
    // 1. 設置 Locale
    Locale locale = new Locale(languageCode); // 如 "zh", "en", "zh_CN"
    Locale.setDefault(locale); // JVM 級默認

    // 2. 更新 Resources Configuration
    Resources res = context.getResources();
    Configuration config = res.getConfiguration();
    config.locale = locale;
    // API 17+ 支持多語言
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLayoutDirection(locale);
    }
    res.updateConfiguration(config, res.getDisplayMetrics());

    // 3. 保存選擇 (SharedPreferences)
    PreferenceManager.getDefaultSharedPreferences(context)
        .edit().putString("app_language", languageCode).apply();
}

完整方案:BaseActivity + 語言持久化 + 重建


// 1. 工具類
public class LocaleHelper {
    public static Context setLocale(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Configuration config = context.getResources().getConfiguration();
        config.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLayoutDirection(locale);
        }
        return context.createConfigurationContext(config);
    }

    public static Context onAttach(Context context) {
        String lang = PreferenceManager.getDefaultSharedPreferences(context)
            .getString("app_language", "en");
        return setLocale(context, lang);
    }
}

// 2. BaseActivity 攔截 attachBaseContext
public class BaseActivity extends AppCompatActivity {
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase));
    }
}

// 3. 語言切換觸發重建
public void changeLanguage(String lang) {
    LocaleHelper.setLocale(this, lang);
    recreate(); // 關鍵:重建 Activity 使佈局/字符串重新加載
}

Android 7.0 (API 24+) 多語言適配


@RequiresApi(api = Build.VERSION_CODES.N)
public static Context updateLocale(Context context, Locale locale) {
    Configuration config = context.getResources().getConfiguration();
    LocaleList localeList = new LocaleList(locale);
    config.setLocales(localeList);
    config.locale = locale; // 兼容舊 API
    return context.createConfigurationContext(config);
}

Spinner 語言選擇器示例


String[] languages = {"English", "中文", "日本語"};
String[] codes = {"en", "zh", "ja"};

ArrayAdapter adapter = new ArrayAdapter<>(this, 
    android.R.layout.simple_spinner_item, languages);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override public void onItemSelected(AdapterView parent, View view, int pos, long id) {
        changeLanguage(codes[pos]);
    }
    @Override public void onNothingSelected(AdapterView parent) {}
});

// 初始化選中項
String saved = prefs.getString("app_language", "en");
for (int i = 0; i < codes.length; i++) {
    if (codes[i].equals(saved)) { spinner.setSelection(i); break; }
}

注意事項與最佳實踐

項目建議
重建 Activityrecreate() 會銷毀重建,確保 ViewModel/onSaveInstanceState 處理狀態保存
系統語言跟隨提供「跟隨系統」選項,清除 SP 中語言設置,attachBaseContext 讀取不到時用系統默認
右到左語言 (RTL)config.setLayoutDirection(locale) + 佈局使用 start/end 而非 left/right
第三方庫部分庫 (如圖片加載) 可能緩存舊語言資源,需配合清理
測試覆蓋 LTR/RTL、多語言字符集、字體縮放、佈局溢出

現代替代:Jetpack DataStore + LocaleConfig (Android 13+)




    
    
    




```
系統設置中自動顯示應用支持的語言選項,用戶可直接在系統級切換。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved