导航抽屉物品面临一个奇怪的问题, 我有3种语言的3种不同的字符串资源,并且在按钮上重新创建活动时,单击活动中的所有文本更改为除导航抽屉项目之外的相应语言,但在下一个娱乐抽屉项目更改并显示之前应显示的值,所以它迟了一步。
这是我的代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (Constants.language==2) {
LocaleHelper.setLocale(this, "ur");
}else if (Constants.language==1){
LocaleHelper.setLocale(this, "hi");
}else if (Constants.language==0){
LocaleHelper.setLocale(this, "en");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_language) {
builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.choose_language));
builder.setPositiveButton("hindi",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Constants.language = 1;
dialog.cancel();
recreate();
}
});
builder.setNeutralButton("english",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Constants.language = 0;
dialog.cancel();
recreate();
}
});
builder.setNegativeButton("urdu",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Constants.language = 2;
dialog.cancel();
recreate();
}
});
builder.create().show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_topstories"
android:icon="@drawable/ic_top_stories"
android:title="@string/top_stories" />
<item
android:id="@+id/nav_india"
android:icon="@drawable/ic_india"
android:title="@string/india" />
<item
android:id="@+id/nav_world"
android:icon="@drawable/world_icon"
android:title="@string/world" />
</group>
</menu>
我已经检查了其他所有内容,例如locale,constant.language和所有3种语言的字符串,活动中的其他文本视图正在正确更新,只有抽屉同步延迟了1。
答案 0 :(得分:5)
如果有人面临同样的问题,这就是我要解决的问题
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer);
我认为即使在重新创建活动时,资源也不会重新加载,因此清除它并再次膨胀它会解决它。
答案 1 :(得分:1)
您必须在调用setContentView()之前更改/加载语言。这适用于使用菜单资源的视图: bottomNavigationView,NavigationDrawer。如果在setContentView()之后更改/加载,则除非重新创建活动,否则不会更改上述视图。
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mBottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeLanguage(getBaseContext());
setContentView(R.layout.activity_main);
}
public void changeLanguage(Context context, String languageToload) {
Locale locale = new Locale(languageToload);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}