Android switching between Chinese and English to resolve the weight loss conflict

Keywords: Mobile xml Android encoding Gradle

Follow the steps

I. AndroidManifest.xml file (manifest file)

Add:

android:configChanges="locale"

<activity
            android:name="com.activity.RepairActivity"
            android:configChanges="locale"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" >
        </activity>

2. Create an English strings.xml file with the directory value-en

values/strings.xml is the default language. If strings.xml does not exist in the values-en directory, then English will choose the default language.

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="app_name">language</string>
     <string name="action_settings">Settings</string>
    <string name="alert_data_init">updating..</string>
 </resources>

3. Core Code

/**
     * Refresh language
     */
    public void updateActivity(String sta) {

        // Local Language Settings
        Locale myLocale = new Locale(sta);
        Resources res = getResources();// Get res resource objects
        DisplayMetrics dm = res.getDisplayMetrics();// Get screen parameters: mainly resolution, pixels, etc.
        Configuration conf = res.getConfiguration();// Get the settings object
        conf.locale = myLocale;// Simplified Chinese
        res.updateConfiguration(conf, dm);
        // startActivity((new Intent(this, TabActivity.class)));
        // this.finish();
    }

String sta= getResources().getConfiguration().locale.getCountry()// Used to determine the current language

Locale.getDefault().getLanguage()// Language: For example, Chinese is zh, English is en, Japanese is ko;

Locale.getDefault().toString()// Specific categories: for example, traditional zh_TW, simplified zh_CN. There is en_GB in English and ko_KR in Japanese.

Pass the language corresponding to Strings.xml that needs to be switched into the updateActivity(String sta) method...

 

You can also call this class

 

// Switching to English
                if (LanguageSettings.getInstance().getCurrentLanguage()
                        .equals("zh")) {
                    updateActivity("en");
                    LanguageSettings.getInstance().switchCurrentLanguage();
                } else {
                    // Switch to Chinese
                    updateActivity("zh");
                    LanguageSettings.getInstance().switchCurrentLanguage();
                }

Language Settings. getInstance (). getCurrentLanguage () Identification column for dynamic assignment of variables for judgment! (Using the singleton model)

I wrote this myself:

private void checkLanguage() {
        // Judge before the interface comes out
        String aString = SystemArgs.LANGUAGE;
        System.out.print(aString);
        System.out.print(SystemArgs.LANGUAGE);
        String language = pref.getString(SystemArgs.LANGUAGE, "Chinese");
        System.out.print(language);
        Log.i("a", language);
        try {
            if (language.equalsIgnoreCase("Chinese") || language.equals("Chinese"))
                chooseLanguage(Locale.CHINA);
            else if (language.equalsIgnoreCase("English")
                    || language.equals("English"))
                chooseLanguage(Locale.US);
        } catch (Exception e) {
            // Log.i("error", "language selection error");
        }

    }

 

private void chooseLanguage(Locale locale) {
        Resources resources = getResources();// Get res resource objects

        Configuration config = resources.getConfiguration();// Get the settings object

        DisplayMetrics dm = resources.getDisplayMetrics();// Get screen parameters: mainly resolution, pixels, etc.

        config.locale = locale; // language

        resources.updateConfiguration(config, dm);
    }

For the change of picture, you need to judge the language in oncreate and display what picture.

IV. Other Records

Set default values for ListPreference Language Options

try {
            if (language.equalsIgnoreCase("Chinese") || language.equals("Chinese"))
                CLanguage_ListPreference.setValueIndex(0);
            else if (language.equalsIgnoreCase("English")
                    || language.equals("English"))
                CLanguage_ListPreference.setValueIndex(1);
        } catch (Exception e) {
            // Log.i("error", "language selection error");
        }

 

String language = defaultSharedPreferences.getString(
                SystemArgs.LANGUAGE, "Chinese");
        ImageView button = null;
        // Pictures have to decide their own language choices
        if (language.equalsIgnoreCase("Chinese") || language.equals("Chinese"))

When you do this, you encounter a conflict. When you set the following attributes in build.gradle, you will have unsuccessful switching.

Because APP slimming restricts res to display only Chinese, it causes conflicts and the switch is unsuccessful.

   defaultConfig {
        targetSdkVersion 28
        resConfig "zh"
    //Many of the attributes here are unaffected and omitted.
  }

Solution

delete

  resConfig "zh"

perhaps

resConfig "zh","en"

Can solve the problem, if there are any other problems, you can contact the author at any time

Posted by Megahertza on Thu, 24 Jan 2019 04:54:13 -0800