我对我的应用程序使用一个活动多个片段的方法,并且我有一个嵌入在一个活动的BottomNavigationBar布局文件中。此外,我还有一个用于语言选择的片段(FR_LanguageSelection)。基本上,对于我的所有片段,语言选择都是正常工作的,不幸的是,当我在FR中选择另一种语言时,BottomNavigationBar的字符串资源(android:title = "@ string / language“和android:title = "@string/Back")的语言不会改变_LanguageSelection,尽管字符串-XML-文件中的资源是存在的。
现在我想问你,当我在FR中选择另一种语言时,你是否有在每个片段中调整语言的线索_LanguageSelection,而BottomNavigationBar的语言保持不变?我将感谢您的每一条评论,并将非常感谢您的帮助。
在这里您可以看到BottomNavigationBar的XML文件
在这里,您可以看到用于选择语言FR的片段的Java文件_
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());
}
}
}提醒:你知道我该怎么解决这个问题吗?
发布于 2021-02-26 14:18:01
尝试添加recreate()
public void onClick(View view) {
...
recreate()
}我猜,您是在更新配置,但并没有强制activity使用这些新的配置。呼叫recreate()应触发onConfiguration更改事件并强制activity使用新配置。
发布于 2021-02-26 02:32:11
清空再充气menu的BottomNavigationView每次切换语言时:
myBottomNavigation.getMenu().clear();
myBottomNavigation.inflateMenu(R.menu.my_bottom_nav_menu);https://stackoverflow.com/questions/66300781
复制相似问题