首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同语言的字符串资源在BottomNavigationBar中不起作用

不同语言的字符串资源在BottomNavigationBar中不起作用
EN

Stack Overflow用户
提问于 2021-02-21 17:19:32
回答 2查看 86关注 0票数 0

我对我的应用程序使用一个活动多个片段的方法,并且我有一个嵌入在一个活动的BottomNavigationBar布局文件中。此外,我还有一个用于语言选择的片段(FR_LanguageSelection)。基本上,对于我的所有片段,语言选择都是正常工作的,不幸的是,当我在FR中选择另一种语言时,BottomNavigationBar的字符串资源(android:title = "@ string / language“和android:title = "@string/Back")的语言不会改变_LanguageSelection,尽管字符串-XML-文件中的资源是存在的。

现在我想问你,当我在FR中选择另一种语言时,你是否有在每个片段中调整语言的线索_LanguageSelection,而BottomNavigationBar的语言保持不变?我将感谢您的每一条评论,并将非常感谢您的帮助。

在这里您可以看到BottomNavigationBar的XML文件

在这里,您可以看到用于选择语言FR的片段的Java文件_

LanguageSelection:

代码语言:javascript
复制
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());
        }

        }




}

提醒:你知道我该怎么解决这个问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-26 14:18:01

尝试添加recreate()

代码语言:javascript
复制
public void onClick(View view) {
    ...
  recreate()
}

我猜,您是在更新配置,但并没有强制activity使用这些新的配置。呼叫recreate()应触发onConfiguration更改事件并强制activity使用新配置。

票数 1
EN

Stack Overflow用户

发布于 2021-02-26 02:32:11

清空再充气menuBottomNavigationView每次切换语言时:

代码语言:javascript
复制
myBottomNavigation.getMenu().clear();
myBottomNavigation.inflateMenu(R.menu.my_bottom_nav_menu);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66300781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档