如何在ImageView上的Android应用中更改语言,然后在Android中单击

时间:2019-01-28 06:28:27

标签: android navigation-drawer locale android-tablayout

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

如果用户单击工具栏中的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;

    }

}

Here I've attached the screenshot for my locale file structure

3 个答案:

答案 0 :(得分:1)

应用语言环境更改后,尝试调用activity.recreate()。这将允许使用新实例重新创建活动。您可以查看文档here

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

答案 1 :(得分:-1)

要了解有关recreate()活动方法的更多信息,请检查下面的给定链接: https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby

答案 2 :(得分:-1)

添加我在代码中提到的recreate方法

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services
        services.AddHealthChecks();

        services.AddMvc();

        // Register your consumers if the need dependencies
        services.AddScoped<SomeDependency>()
        services.AddScoped<OrderConsumer>();

        // Register MassTransit
        services.AddMassTransit(
            provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host("localhost", host => 
                { 
                    host.Username("guest");
                    host.Password("guest");
                });

                cfg.ReceiveEndpoint(host, "submit-order", ep =>
                {
                    ep.PrefetchCount = 16;
                    ep.UseMessageRetry(x => x.Interval(2, 100));

                    ep.Consumer<OrderConsumer>(provider);
                });
            }),
            x => x.AddConsumer<OrderConsumer>());
    }

    // everything else
}