android应用程序语言在任何地方都会发生变化,但在菜单和选项卡布局标题中不会发生变化

uyhoqukh  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(288)

我遇到了一个非常奇怪的问题。我想更改我的应用程序的语言,为此我使用了在stackoverflow上找到的代码:

private void restartActivityInLanguage(String language) {
    Locale locale = new Locale(language);
    Configuration config = new Configuration();
    config.locale = locale;
    Resources resources = getResources();
    resources.updateConfiguration(config, resources.getDisplayMetrics());
    getActivity().recreate();
}

    polishLanguage = view.findViewById(R.id.polish_language);
    englishLanguage = view.findViewById(R.id.english_language);

    polishLanguage.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            restartActivityInLanguage("pl");

        }
    });

    englishLanguage.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            restartActivityInLanguage("en");
        }
    });

此代码与我的应用程序的主要部分完美配合,但有些地方的文字不会更改,并与我的整个手机保持相同的语言版本,这些地方是:
弹出菜单

holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Context wrapper = new ContextThemeWrapper(context, R.style.PopupMenu);
          PopupMenu popup = new PopupMenu(wrapper, v);
          popup.inflate(R.menu.browse_location_menu);

          popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
              @Override
              public boolean onMenuItemClick(MenuItem item) {
                  switch (item.getItemId()) {
                      case R.id.browse_location_edit:
                          onProductClick.onClick(locationModel);
                          break;
                      case R.id.browse_location_delete:
                          onProductClick.onDelete(locationModel);
                          break;
                      default:
                          break;
                  }
                  return true;
              }
          });
          popup.show();
      }
  });
<item
    android:id="@+id/browse_location_edit"
    android:title="@string/browse_location_edit" />

<item
    android:id="@+id/browse_location_delete"
    android:title="@string/browse_location_delete"/>

-表格标题

TabLayout tabLayout = findViewById(R.id.tab_layout);
    new TabLayoutMediator(tabLayout, viewPager,
            (tab, position) ->
            {
                switch (position) {
                    case 0:
                        tab.setText(getString(R.string.add_product));
                        break;
                    case 1:
                        tab.setText(getString(R.string.add_location));
                        break;
                }
            }
    ).attach();

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/TabLayoutHeight"
    android:background="@color/IvoryBackground"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

我正在使用string.xml文件中的文本,该文件包含两种语言版本的字符串。
我已经想不出是什么引起了这个问题。logcat没有错误,文本没有改变。如有任何帮助和建议,我们将不胜感激:)

z9zf31ra

z9zf31ra1#

你可以尝试玩一些东西:
第一:您使用的上下文:

getString(R.string.my_string);
getApplication().getString(R.string.my_string);
getApplicationContext().getString(R.string.my_string);
getBaseContext().getString(R.string.my_string);

第二:获取字符串的方法:

Resources.getSystem().getString(R.string.my_string);
getResources().getString(R.string.my_string);
getString(R.string.my_string);

不久前我就面对了这个问题,第一种方法对我很有效。

ccrfmcuu

ccrfmcuu2#

在TableLayout中,唯一可行的解决方案是:

getBaseContext().getString(R.string.my_string);

弹出菜单更棘手,因为它们是片段中使用的适配器,而zain解决方案不起作用。我有一个解决方案:android:在viewpageradapter中如何获取getbasecontext(),我以前的代码是:

locationsAdapter = new LocationsAdapterDB(getActivity().getApplicationContext(),

我把它改成:

locationsAdapter = new LocationsAdapterDB(getActivity().getBaseContext(),

在适配器类中,我添加了:

public class Adapter extends PagerAdapter {
Context mContext;

public Adapter(Context context) {
    mContext = context;
}

}

相关问题