不同语言的字符串资源在 BottomNavigationBar 中不起作用
String resources for different languages not working in BottomNavigationBar
我为我的应用程序使用一个-Activity-多个片段方法,并且我有一个嵌入在 XML 布局文件中的 BottomNavigationBar activity。此外,我还有一个用于语言选择的片段 (FR_LanguageSelection)。基本上,语言选择适用于我的所有片段,不幸的是 BottomNavigationBar 的字符串资源的语言(android:title = "@string/Language" 和 android:title = "@string/Back") 当我在 FR_LanguageSelection 中选择另一种语言时不要更改,尽管字符串中的资源-XML-文件存在。
现在我想问你是否知道我在 FR_LanguageSelection 选择另一种语言时在每个片段中调整了哪些语言,而 BottomNavigationBar 的语言保持不变?我会感谢每一条评论,非常感谢你的帮助。
在这里您可以看到 BottomNavigationBar 的 XML-文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon = "@drawable/ic_baseline_arrow_left_24"
android:title = "@string/Back" />
<item
android:id="@+id/FR_LanguageSelection"
android:icon = "@drawable/ic_add_circle_full"
android:title = "@string/Language" />
<item
android:id="@+id/Fragment1"
android:icon = "@drawable/ic_add_circle_full"
android:title = "Fragment1" />
</menu>
在这里您可以看到用于选择语言的片段的 Java 文件 FR_LanguageSelection:
package com.example.td.bapp;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.td.bapp.databinding.FragmentLanguageSelectionBinding;
import java.util.Locale;
/**
Fragment for selecting the language of the app via ImageButtons
*
*/
public class FR_LanguageSelection extends Fragment implements View.OnClickListener {
/*
String specifying the language of the App
*/
public static String currentLanguageOfTheApp;
public static final String LANGUAGE_GERMAN = "German";
public static final String LANGUAGE_ENGLISH = "English";
public FR_LanguageSelection() {
// Required empty public constructor
}
public static FR_LanguageSelection newInstance(String param1, String param2) {
FR_LanguageSelection fragment = new FR_LanguageSelection();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private FragmentLanguageSelectionBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLanguageSelectionBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.imageButtonGermany.setOnClickListener(this);
binding.imageButtonUK.setOnClickListener(this);
}
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onClick(View view) {
if(view.getId() == R.id.imageButtonGermany) {
/*
Set the language to "German" for other fragments and database queries
*/
this.currentLanguageOfTheApp = LANGUAGE_GERMAN;
/*
Set the language to "German" for the XML-layout files
*/
Locale locale;
locale = new Locale("de", "DE");
Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
Locale.setDefault(locale);
config.setLocale(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config,
getActivity().getBaseContext().getResources().getDisplayMetrics());
Navigation.findNavController(view).navigate
(FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());
}
if(view.getId() == R.id.imageButtonUK) {
/*
Set the language to "English" for other fragments and database queries
*/
this.currentLanguageOfTheApp = LANGUAGE_ENGLISH;
/*
Set the language to "English" for the XML-layout files
*/
Locale locale;
locale = new Locale("en", "EN");
Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
Locale.setDefault(locale);
config.setLocale(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config,
getActivity().getBaseContext().getResources().getDisplayMetrics());
Navigation.findNavController(view).navigate(FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());
}
}
}
提醒:知道如何解决这个问题吗?
每次切换语言时清除 BottomNavigationView
的 menu
并重新膨胀:
myBottomNavigation.getMenu().clear();
myBottomNavigation.inflateMenu(R.menu.my_bottom_nav_menu);
尝试添加 recreate()
public void onClick(View view) {
...
recreate()
}
我猜,您是在更新配置,而不是强制 activity 使用这些新配置。
调用 recreate()
应触发 onConfiguration 更改事件并强制 activity 使用新配置。
我为我的应用程序使用一个-Activity-多个片段方法,并且我有一个嵌入在 XML 布局文件中的 BottomNavigationBar activity。此外,我还有一个用于语言选择的片段 (FR_LanguageSelection)。基本上,语言选择适用于我的所有片段,不幸的是 BottomNavigationBar 的字符串资源的语言(android:title = "@string/Language" 和 android:title = "@string/Back") 当我在 FR_LanguageSelection 中选择另一种语言时不要更改,尽管字符串中的资源-XML-文件存在。
现在我想问你是否知道我在 FR_LanguageSelection 选择另一种语言时在每个片段中调整了哪些语言,而 BottomNavigationBar 的语言保持不变?我会感谢每一条评论,非常感谢你的帮助。
在这里您可以看到 BottomNavigationBar 的 XML-文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon = "@drawable/ic_baseline_arrow_left_24"
android:title = "@string/Back" />
<item
android:id="@+id/FR_LanguageSelection"
android:icon = "@drawable/ic_add_circle_full"
android:title = "@string/Language" />
<item
android:id="@+id/Fragment1"
android:icon = "@drawable/ic_add_circle_full"
android:title = "Fragment1" />
</menu>
在这里您可以看到用于选择语言的片段的 Java 文件 FR_LanguageSelection:
package com.example.td.bapp;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.td.bapp.databinding.FragmentLanguageSelectionBinding;
import java.util.Locale;
/**
Fragment for selecting the language of the app via ImageButtons
*
*/
public class FR_LanguageSelection extends Fragment implements View.OnClickListener {
/*
String specifying the language of the App
*/
public static String currentLanguageOfTheApp;
public static final String LANGUAGE_GERMAN = "German";
public static final String LANGUAGE_ENGLISH = "English";
public FR_LanguageSelection() {
// Required empty public constructor
}
public static FR_LanguageSelection newInstance(String param1, String param2) {
FR_LanguageSelection fragment = new FR_LanguageSelection();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private FragmentLanguageSelectionBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLanguageSelectionBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.imageButtonGermany.setOnClickListener(this);
binding.imageButtonUK.setOnClickListener(this);
}
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onClick(View view) {
if(view.getId() == R.id.imageButtonGermany) {
/*
Set the language to "German" for other fragments and database queries
*/
this.currentLanguageOfTheApp = LANGUAGE_GERMAN;
/*
Set the language to "German" for the XML-layout files
*/
Locale locale;
locale = new Locale("de", "DE");
Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
Locale.setDefault(locale);
config.setLocale(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config,
getActivity().getBaseContext().getResources().getDisplayMetrics());
Navigation.findNavController(view).navigate
(FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());
}
if(view.getId() == R.id.imageButtonUK) {
/*
Set the language to "English" for other fragments and database queries
*/
this.currentLanguageOfTheApp = LANGUAGE_ENGLISH;
/*
Set the language to "English" for the XML-layout files
*/
Locale locale;
locale = new Locale("en", "EN");
Configuration config = new Configuration(getActivity().getBaseContext().getResources().getConfiguration());
Locale.setDefault(locale);
config.setLocale(locale);
getActivity().getBaseContext().getResources().updateConfiguration(config,
getActivity().getBaseContext().getResources().getDisplayMetrics());
Navigation.findNavController(view).navigate(FR_LanguageSelectionDirections.actionFRLanguageSelectionToFRMenu());
}
}
}
提醒:知道如何解决这个问题吗?
每次切换语言时清除 BottomNavigationView
的 menu
并重新膨胀:
myBottomNavigation.getMenu().clear();
myBottomNavigation.inflateMenu(R.menu.my_bottom_nav_menu);
尝试添加 recreate()
public void onClick(View view) {
...
recreate()
}
我猜,您是在更新配置,而不是强制 activity 使用这些新配置。
调用 recreate()
应触发 onConfiguration 更改事件并强制 activity 使用新配置。