• 安卓开发 多语言


    一、切换系统语言APP崩溃的问题。

    方法一、android:configChanges="locale|layoutDirection"
        在清单文件中每个activity中添加该属性,可以避免app处于后台时,切换了系统语言后,将app唤醒到前台时,应用崩溃的bug。
    二、多语言。
       提取出来的方法如下:
    public static void qiChangeLauage(Context context,int type,boolean isrestart){
    if (isrestart){
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);
    // 杀掉进程
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(0);
    }else{
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    // 应用用户选择语言
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){
    config.setLocale(getSetLocale(type));//8.0以上使用
    }else{
    config.locale = getSetLocale(type);
    }
    resources.updateConfiguration(config, dm);//这个方法在8.0以后没有效果,需要使用context.createConfigurationContext(configuration),具体使用方法在attachBaseContext中体现。
    }
    }

    public static Locale getSetLocale(int type){
    switch (type){
    case 0:
    return Locale.SIMPLIFIED_CHINESE;
    case 1:
    return Locale.TRADITIONAL_CHINESE;
    case 2:
    return Locale.ENGLISH;
    default:
    return Locale.SIMPLIFIED_CHINESE;
    }
    }

    public static Context attachBaseContext(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 8.0需要使用createConfigurationContext处理
    return updateResources(context);
    } else {
    return context;
    }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context updateResources(Context context) {
    Resources resources = context.getResources();
    Locale locale = getSetLocale(MyApplication.getInstance().getLauageType());// getSetLocale方法是获取新设置的语言
    Configuration configuration = resources.getConfiguration();
    configuration.setLocale(locale);
    configuration.setLocales(new LocaleList(locale));
    return context.createConfigurationContext(configuration);
    }
    具体使用:
    1、在Application的oncreat方法中使用。设置多语言时,尽量使用getApplicationContext()。
    2、在
    Application的以下方法中使用
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    *****.qiChangeLauage(this,getLauageType(),false);
    }
    这一步可要可不要,该方法的目的是获取系统设置改变时,做出的相应的处理操作。这里的系统设置譬如:语言切换,屏幕旋转等
    3、语言切换的代码,在高版本中废弃了updateConfiguration方法,替代方法为createConfigurationContext,该方法是返回一个Context,也就是语言需要植入到Context中,每个Context都植入一遍。所以在BaseActivity中需要加入如下代码
    @Override
    protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(****.attachBaseContext(newBase));
    }
    这一步必须有,否则8.0以下的语言设置无效。
  • 相关阅读:
    作业2 求题目中的数
    2013 C#单元测试
    实现项目WC
    带括号多项式版四则运算
    20道简单加减法随机生成程序扩展版体会
    20道简单加减法随机生成程序
    Jeesite 集成微信支付接口
    第一节:JAVA 语言的学习目标
    vector(未完)
    关于phpstorm端口63342的修改经历
  • 原文地址:https://www.cnblogs.com/qynprime/p/9233914.html
Copyright © 2020-2023  润新知