更改应用的区域设置时TextView不会更改

时间:2018-04-12 07:18:16

标签: android locale

我在从locale选择选项时尝试更改应用的onOptionsItemSelected

当我尝试这样做时,语言会在同一菜单中成功更改。

但是,布局中存在的text中的TextView并未发生变化。

感谢任何帮助。

我的主要活动是:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Locale myLocale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView = (TextView) findViewById(R.id.textView1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_english) {
            Toast.makeText(getApplicationContext(),
                    "You have selected English", Toast.LENGTH_SHORT)
                    .show();
            setLocale("en");
            return true;
        } else if (id == R.id.action_nepal) {
            Toast.makeText(getApplicationContext(),
                    "You have selected Nepali", Toast.LENGTH_SHORT)
                    .show();
            setLocale("ne");
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


    public void setLocale(String lang) {

        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        getBaseContext().getResources().updateConfiguration(conf,
                getBaseContext().getResources().getDisplayMetrics());
        invalidateOptionsMenu();
        Intent refresh = new Intent(this, MainActivity.class);
        startActivity(refresh);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // refresh your views here
        super.onConfigurationChanged(newConfig);
    }


}

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.languagesupport">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:configChanges="locale"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的两个字符串文件:

英语:

<resources>
    <string name="app_name">languageChange</string>
    <string name="sample_text">Hello World</string>
    <string name="action_settings">Settings</string>
    <string name="english">English</string>
    <string name="nepal">Nepal</string>
</resources>

尼泊尔语:

<resources>
    <string name="app_name">भीमदत्त नगरपालिका</string>
    <string name="action_settings">गृहपृष्ठ</string>
    <string name="sample_text">" विकास क्षेत्रमा पर्ने महाकाली अञ्चल), कञ्चनपुर जिल्लाको स"</string>
    <string name="english">अंग्रेजी</string>
    <string name="nepal">नेपाल</string>
</resources>

2 个答案:

答案 0 :(得分:1)

重新启动活动后尝试使用 finish();

Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();

答案 1 :(得分:0)

有两种不同的方法:

<强> 1。没有重新启动活动:

  • 在清单中的android:configChanges="locale"

  • 中设置<activity>
  • 按以下方式更改setLocale:

        private void setLocale(String lang) {
            Locale myLocale = new Locale(lang);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
            invalidateOptionsMenu();
            onConfigurationChanged(conf);//Add this line
        }
    
  • 覆盖onConfigurationChanged()

        @Override
        public void onConfigurationChanged(final Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            textView.setText(<your-text>);
            //Any other UI text to change
        }
    

这将确保您的语言环境得到正确更改。

<强> 2。重新创建活动:

而不是

Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();

拨打

recreate();

这将导致使用新实例重新创建此Activity。