zoukankan      html  css  js  c++  java
  • 安卓开发 多语言

    一、切换系统语言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以下的语言设置无效。
  • 相关阅读:
    fetch jsonp请求接口
    mysql explain执行计划详解
    MySQL主从复制与读写分离 --非原创
    C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
    Mysql有没有语法可以在增加列前进行判断该列是否存在
    .net4.0注册到IIS ,重新注册IIS ,iis注册
    C#操作IIS程序池及站点的创建配置
    .net C# 对虚拟目录IIS的操作
    I​n​n​o​ ​s​e​t​u​p​ ​常​用​修​改​技​巧
    innosetup语法详解
  • 原文地址:https://www.cnblogs.com/qynprime/p/9233914.html
Copyright © 2011-2022 走看看