如何在更改语言时刷新布局

时间:2014-01-13 07:01:32

标签: android listview

我有ListView和多个布局,当我点击ListView项目时,所有布局都已设置,每个项目点击包含不同的布局,我选择了两种语言通过一个布局上的单选按钮。之后,当我点击项目字符串时,刷新但ImageButton不会使用不同的语言刷新。我已经有了两种语言的图像,我的所有布局都保存在一个数组中。

我的代码如下:

在语言选择后点击保存按钮时:

if (lang_selected.equalsIgnoreCase("English")) 

{

    Locale locale = new Locale("es");

Locale.setDefault(locale);

Configuration config = new Configuration();

config.locale = locale; 
                            context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

//initView(view);

}else if(lang_selected.equalsIgnoreCase("Chinese"))

        {


Locale locale = new Locale("zh");

Locale.setDefault(locale);

Configuration config = new Configuration();

config.locale = locale; 
                            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
                            //initView(view);
                        }

当我点击listview项目时:

protected void onGeneralInfoItemClick(AdapterView<?> parent,
            final int position) {

        final GeneralInfoData obj = (GeneralInfoData) parent
                .getItemAtPosition(position);

        final View layoutView = obj.getDataLayoutView();

        detailCantiner.removeAllViews();

        detailCantiner.addView(layoutView);

        try{
            if(position==0)
            {
                tvTradeName.setText(R.string.trade);
                btnSaveTrade.setImageResource(R.drawable.general_info_save_button);

                String 
str=Util.getSharedPreference(context).getString("UserCompany", "");

                trade_name.setText(str);

            }if(position==1).......................**

2 个答案:

答案 0 :(得分:3)

您可以使用以下代码,在代码中适用的地方使用这些功能。

For language change....

       public void changeLang(String lang) {
        if (lang.equalsIgnoreCase(""))
            return;
        iocLocale = new Locale(lang);
        Log.v("My Language",iocLocale + "");
        saveLocale(lang);

        Locale.setDefault(iocLocale);

        android.content.res.Configuration config = new android.content.res.Configuration();

        config.locale = iocLocale;

        getResources().updateConfiguration(config,
                getResources().getDisplayMetrics());


    }
For refreshing text....


    private void refreshTextchange() 
    {
        txt_home.setText(getResources().getString(R.string.home));
        txt_patient.setText(getResources().getString(R.string.Patientt));
        txt_staff.setText(getResources().getString(R.string.Stafff));
        txt_makechange.setText(getResources().getString(R.string.MakeChangeAppointment));
        txt_map.setText(getResources().getString(R.string.Map));
        txt_contact.setText(getResources().getString(R.string.Contact));



    }

For saving the language....


    public void saveLocale(String lang) {
        SharedPreferences prefs = getSharedPreferences("com.ioc",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("language", lang);
        editor.commit();
    }

For loading language i.e., first time you run your code...

    public void loadLocale() {
        SharedPreferences prefs = getSharedPreferences("com.ioc",
                Activity.MODE_PRIVATE);
        String language = prefs.getString("language", "");
        changeLang(language);
        iocLocale = new Locale(language);
        Log.v("My Language",language + "");
        Log.d("c", "333333333");
    }

For checking language...

    private boolean checkLocal()
    {
        SharedPreferences prefs = getSharedPreferences("com.ioc",
                Activity.MODE_PRIVATE);
        String language = prefs.getString("language", "");
        if(language.equalsIgnoreCase("pt"))
            return true;
        else
            return false;

    }

答案 1 :(得分:1)

尝试这样做

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
    setContentView(R.layout.main);
    setTitle(R.string.app_name);

    // Checks the active language
    if (newConfig.locale == Locale.ENGLISH) {
        Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
    } else if (newConfig.locale == Locale.FRENCH){
        Toast.makeText(this, "French", Toast.LENGTH_SHORT).show();
    }
}

并按如下方式使用

Configuration newConfig = new Configuration();
             newConfig.locale = Locale.FRENCH;
             onConfigurationChanged(newConfig);