好的伙计们。我正在做一个项目,我在操作栏中放了一个按钮,从保加利亚语翻译成英语,反之亦然。我在区域设置中进行了更改,但问题是在单击按钮后,应用程序中的语言发生了变化,但操作栏中按钮上的文本没有更改。我已经创建了两个文件夹值-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块?
答案 0 :(得分:0)
更改区域设置后调用invalidateOptionsMenu()...或只是重新启动应用程序:)