Android 更改区域设置后更改文本
好的,伙计们。我正在做一个项目,我在动作栏上放了一个按钮,它可以从保加利亚语翻译成英语,反之亦然。我已经在区域设置中进行了更改,但我的问题是,在我单击按钮后,应用程序中的语言会更改,但操作栏中按钮上的文本不会更改。我创建了两个文件夹值bg和en,我为按钮使用的字符串在string.xmls中声明,但文本根本没有更改 代码如下:Android 更改区域设置后更改文本,android,android-actionbar,locale,Android,Android Actionbar,Locale,好的,伙计们。我正在做一个项目,我在动作栏上放了一个按钮,它可以从保加利亚语翻译成英语,反之亦然。我已经在区域设置中进行了更改,但我的问题是,在我单击按钮后,应用程序中的语言会更改,但操作栏中按钮上的文本不会更改。我创建了两个文件夹值bg和en,我为按钮使用的字符串在string.xmls中声明,但文本根本没有更改 代码如下: import java.util.Locale; import com.pushbots.push.Pushbots; import android.net.Uri;
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()充气(R.menu.main,menu);不知怎的在我的If区 在更改区域设置后调用InvalidateOptions菜单(),或者只需重新启动应用:)可能有很多事情。请张贴您的密码。非常感谢。这就是我要找的。这可能很简单,但我是android新手,很高兴你帮了我:)