好吧,夥計們。我正在做一個項目,我在操作欄中放了一個按鈕,從保加利亞語翻譯成英語,反之亦然。我在語言環境中進行了更改,但是我的問題是,在單擊該按鈕之後,應用程序中的語言會發生變化,但操作欄中按鈕上的文本不會變化。我已經創建了兩個文件夾values-bg和values-en,並且我爲這個按鈕所使用的字符串在兩個string.xmls中聲明,但是文本根本不會改變。更改語言環境後更改文本
這是代碼:
import java.util.Locale;
import com.pushbots.push.Pushbots;
import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button lBtn, sBtn, cBtn, aBtn;
TextView hLink;
private String loc = "loc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#118cec")));
setContentView(R.layout.activity_main);
this.lBtn = (Button) findViewById(R.id.button1);
lBtn.setOnClickListener(this);
this.sBtn = (Button) findViewById(R.id.button2);
sBtn.setOnClickListener(this);
this.cBtn = (Button) findViewById(R.id.button3);
cBtn.setOnClickListener(this);
this.aBtn = (Button) findViewById(R.id.button4);
aBtn.setOnClickListener(this);
this.hLink = (TextView) findViewById(R.id.hlink);
hLink.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == lBtn.getId()) {
startActivity(new Intent(this, lect_list.class));
Log.d(loc, "pressed");
} else if (v.getId() == sBtn.getId()) {
Toast t = Toast.makeText(this, "Text",
Toast.LENGTH_LONG);
t.show();
} else if (v.getId() == cBtn.getId()) {
Toast t = Toast.makeText(this,
"Text",
Toast.LENGTH_LONG);
t.show();
} else if (v.getId() == aBtn.getId()) {
startActivity(new Intent(this, dlect.class));
Log.d(loc, "pressed");
}
else if (v.getId() == hLink.getId()) {
Toast t = Toast.makeText(this, "Text",
Toast.LENGTH_LONG);
t.show();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == R.id.trans) {
Toast t = Toast.makeText(this, "Translation", Toast.LENGTH_LONG);
t.show();
Locale mLocale = new Locale("bg");
Locale.setDefault(mLocale);
Configuration config = getBaseContext().getResources()
.getConfiguration();
if (!config.locale.equals(mLocale)) {
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
null);
setContentView(R.layout.activity_main);
this.lBtn = (Button) findViewById(R.id.button1);
lBtn.setOnClickListener(this);
this.sBtn = (Button) findViewById(R.id.button2);
sBtn.setOnClickListener(this);
this.cBtn = (Button) findViewById(R.id.button3);
cBtn.setOnClickListener(this);
this.aBtn = (Button) findViewById(R.id.button4);
aBtn.setOnClickListener(this);
} else {
mLocale = new Locale("en");
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
null);
setContentView(R.layout.activity_main);
this.lBtn = (Button) findViewById(R.id.button1);
lBtn.setOnClickListener(this);
this.sBtn = (Button) findViewById(R.id.button2);
sBtn.setOnClickListener(this);
this.cBtn = (Button) findViewById(R.id.button3);
cBtn.setOnClickListener(this);
this.aBtn = (Button) findViewById(R.id.button4);
aBtn.setOnClickListener(this);
}
}
return super.onOptionsItemSelected(item);
}
}
是否有可能以某種方式誇大菜單一次。在我改變區域設置後,我設置了新的活動,但不是新的菜單。我可以調用getMenuInflater()。inflate(R.menu.main,menu);不知何故在我的If塊?
可能還有很多的事情。請發佈您的代碼。 –