2014-02-06 73 views
1

這裏是改變語言爲什麼在我更改android語言環境時imageview保持不變?

eng.setOnClickListener(setChangeLangListener("en")); 
chi.setOnClickListener(setChangeLangListener("zh")); 


public OnClickListener setChangeLangListener(final String lang) { 
     OnClickListener changeLangListener = new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       Configuration config = new Configuration(getResources() 
         .getConfiguration()); 

       if (lang.equals("en")) { 
        config.locale = Locale.ENGLISH; 
        chi.setTextColor(oldColor); 
        eng.setTextColor(getActivity().getResources().getColor(
          android.R.color.white)); 
       } else { 
        config.locale = Locale.TRADITIONAL_CHINESE; 
        eng.setTextColor(oldColor); 
        chi.setTextColor(getActivity().getResources().getColor(
          android.R.color.white)); 
       } 

       getResources().updateConfiguration(config, 
         getResources().getDisplayMetrics()); 
      } 
     }; 
     return changeLangListener; 
    } 

問題是,當我改變了區域的圖像視圖不刷新以反映更改代碼,我使用tabhost和變化的語言按鈕是片段內,所以我必須改變actvitiy +裏面的片段。如何解決它?謝謝。

的tabhost

tabHost = (FragmentTabHost) findViewById(R.id.tabhost); 
     tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent); 
     tabHost.addTab(
       tabHost.newTabSpec("Home").setIndicator("", 
         res.getDrawable(R.drawable.btn_about)), Home.class, 
       null); 
     tabHost.addTab(
       tabHost.newTabSpec("About").setIndicator("", 
         res.getDrawable(R.drawable.btn_about)), About.class, 
       null); 
     tabHost.addTab(
       tabHost.newTabSpec("Check").setIndicator("", 
         res.getDrawable(R.drawable.btn_check)), Selfie.class, 
       null); 
     tabHost.addTab(
       tabHost.newTabSpec("Gallery").setIndicator("", 
         res.getDrawable(R.drawable.btn_gallery)), 
       PhotoGallery.class, null); 
     tabHost.addTab(
       tabHost.newTabSpec("LeaderBoard").setIndicator("", 
         res.getDrawable(R.drawable.btn_board)), 
       LeaderBoard.class, null); 
     tabHost.addTab(
       tabHost.newTabSpec("Events").setIndicator("", 
         res.getDrawable(R.drawable.btn_events)), Events.class, 
       null); 
     tabHost.getTabWidget().setDividerDrawable(null); 
     setTabBg(tabHost); 

片段

rootView = inflater.inflate(R.layout.home, container, false); 
     loginBtn = (ImageView) rootView.findViewById(R.id.home_connectFB); 
     eng = (TextView) rootView.findViewById(R.id.btn_eng); 
     chi = (TextView) rootView.findViewById(R.id.btn_chi); 
+1

您需要重新加載活動 – gian1200

+0

你介意進一步解釋一下嗎?我不會繼續使用finish(),因爲我使用的是谷歌分析,所以它可能會加倍計算入門率 – user782104

+1

想到的另一個選項是手動重新加載映像。我不在我的電腦裏,所以不能給出正確的答案 – gian1200

回答

0

View.OnClickListener是錯誤的。或者至少是我閱讀它的方式。當你按下eng或chi時,你想改變一些東西。 Imo你應該內聯它,例如成員類,或讓你的活動實現OnClickListenerm,併爲TextView設置相同的內容。例如:

OnClickListener changeLangListener = new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      Configuration config = new Configuration(getResources() 
        .getConfiguration()); 

      if (arg0.getId() = R.id.btn_eng) { 
       config.locale = Locale.ENGLISH; 
       chi.setTextColor(oldColor); 
       eng.setTextColor(getActivity().getResources().getColor(
         android.R.color.white)); 
      } else if (arg0.getId() = R.id.btn_chi) { 
       config.locale = Locale.TRADITIONAL_CHINESE; 
       eng.setTextColor(oldColor); 
       chi.setTextColor(getActivity().getResources().getColor(
         android.R.color.white)); 
      } 

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

eng.setOnClickListener(changeLangListener); 
chi.setOnClickListener(changeLangListener); 
+0

感謝您的幫助。聽衆實際上改變了那兩個文本視圖,問題是其他視圖不相應地改變。您介意提供一些關於如何手動更改視圖的建議嗎?謝謝 – user782104

0

公共無效的setLocale(字符串郎){

myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 

    onConfigurationChanged(conf); 
} 



@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    iv.setImageDrawable(getResources().getDrawable(R.drawable.keyboard)); 
    greet.setText(R.string.greet); 
    textView1.setText(R.string.langselection); 

    super.onConfigurationChanged(newConfig); 

} 
相關問題