Android 应用语言随处更改,但不在菜单和标签布局标题中

问题描述

我遇到了一个很奇怪的问题。我想更改应用程序的语言,为此我使用了在 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 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 中没有错误,只是文本没有改变。任何帮助和建议将不胜感激:)

解决方法

你可以尝试做一些事情:

首先:您使用的上下文:

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

第二:获取String的方法:

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

我前段时间遇到过这种情况,第一种方法对我有用。

,

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

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

弹出菜单更棘手,因为它们是片段中使用的适配器,而 Zain 解决方案不起作用。我有解决方案:Android: In ViewPagerAdapter how to get getBaseContext() 我之前在许多片段之一中的代码是:

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

我改成:

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

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

public class Adapter extends PagerAdapter {
Context mContext;

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

}