【问题标题】:How can I change the language in my Android app on ImageView click in Android如何在 Android 中单击 ImageView 更改我的 Android 应用程序中的语言
【发布时间】:2019-03-03 04:45:46
【问题描述】:

我正在创建一个简单的应用程序,该应用程序需要在单击工具栏中的图像视图时更改语言。该应用程序实际上带有导航抽屉内的选项卡布局。我正在为自己的母语(泰米尔语)使用另一种语言(区域设置)。

如果用户单击工具栏中的 imageView,应用程序的语言应该从英语变为泰米尔语。但是当我点击 imageView 时,应用程序的语言并没有改变。因为我已经在字符串文件中创建了另一个语言环境。我不知道为什么语言没有改变。

MainActivity.java:

public class MainActivity extends AppCompatActivity

        implements NavigationView.OnNavigationItemSelectedListener {



    ImageView imageView;
    Context context;
@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        context = this;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


        setSupportActionBar(toolbar);
        imageView = (ImageView)findViewById(R.id.imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String languageToLoad  = "ta_IN";
                Locale locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

                Toast.makeText(getApplicationContext(),"Language changed",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//it will recreate it self with new language.
                startActivity(intent);

            }
        });






        TabLayout tabLayout =(TabLayout)findViewById(R.id.tabs);

        ViewPager Pager =(ViewPager)findViewById(R.id.viewpager);



        tabpagerAdapter Tabpageradapter = new tabpagerAdapter(getSupportFragmentManager());

        Pager.setAdapter(Tabpageradapter);

        tabLayout.setupWithViewPager(Pager);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(

                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.setDrawerListener(toggle);

        toggle.syncState();



        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);

    }







    @Override

    public void onBackPressed() {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        if (drawer.isDrawerOpen(GravityCompat.START)) {

            drawer.closeDrawer(GravityCompat.START);

        } else {

            super.onBackPressed();

        }

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }



    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();



        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }



        return super.onOptionsItemSelected(item);

    }



    @SuppressWarnings("StatementWithEmptyBody")

    @Override

    public boolean onNavigationItemSelected(MenuItem item) {

        // Handle navigation view item clicks here.

        int id = item.getItemId();



        if (id == R.id.nav_camera) {

            // Handle the camera action

        } else if (id == R.id.nav_gallery) {



        } else if (id == R.id.nav_slideshow) {



        } else if (id == R.id.nav_manage) {



        } else if (id == R.id.nav_share) {



        } else if (id == R.id.nav_send) {



        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawer.closeDrawer(GravityCompat.START);

        return true;

    }

}

:

【问题讨论】:

  • 请也分享 XML 文件

标签: android navigation-drawer locale android-tablayout


【解决方案1】:

应用区域设置更改后,尝试调用activity.recreate()。这将允许使用新实例重新创建活动。你可以查看文档here

startactivity() 的代码替换为recreate()

【讨论】:

  • ...,我是否需要删除意图
  • ., Activity 正在重新创建,但语言没有改变
  • 这是我试过的: imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String languageToLoad = "ta_IN"; Locale locale = new Locale(languageToLoad) ; Locale.setDefault(locale); 配置 config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config,context.getResources().getDisplayMetrics()); recreate(); } })
  • @Thedroider 你在测试什么安卓版本?
  • @KaranMer 在我的 OnePlus 6 的 android 版本 9.0 中
【解决方案2】:

要了解有关活动的 recreate() 方法的更多信息,请查看以下给定链接:- How to call recreate()?

【讨论】:

  • 好的,请尝试使用其他答案明确设置语言,然后尝试保存在共享偏好中选择的当前语言,当您开始活动时,然后检查选择了哪种语言并根据该选择尝试您的要求
【解决方案3】:

添加我在代码中提到的重新创建方法

   String languageToLoad  = "ta_IN";
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
        recreate();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多