美文网首页
Android 国际化笔记

Android 国际化笔记

作者: 727e5d297913 | 来源:发表于2017-04-21 16:25 被阅读0次

resource

  • 是否支持rtl 从右到左布局方式
    在清单文件中配置
    <application
    android:supportsRtl="true">

  • 设置组件xml属性
    android:layoutDirection="ltr"强制设置布局方法

  • 在api17以上使用带有end或start的属性替换带有left或reght的属性 例:android:layout_marginEnd="15dp"

  • 使用不同的限定符名称,提供不同的资源文件
    配置限定符<a href='https://developer.android.com/guide/topics/resources/providing-resources.html'>完整列表</a>(需要科学上网)

配置 示例 说明
MCC和MNC mcc310 移动国家代码
语言和区域 en 语言通过由两个字母组成的<a href='http://www.loc.gov/standards/iso639-2/php/code_list.php'>ISO 639-1</a>语言代码定义
布局方向 ldrtl</br>ldltr ldltr是默认值,要为应用启用从右到左的布局功能,必须将 supportsRtl 设置为 "true",并将 targetSdkVersion 设置为 17 或更高版本
  • 命名规则
    • 您可以为单组资源指定多个限定符,并使用短划线分隔。例如,drawable-en-rUS-land 适用于横排美国英语设备。
    • 限定符出现的顺序必须遵循表中列出的顺序
    • 不能嵌套备用资源目录。例如,您不能拥有 res/drawable/drawable-en/
    • 值不区分大小写
    • 对于每种限定符类型,仅支持一个值

处理配置变更##

  • 当配置变更时会调用这个方法 onConfigurationChanged(Configuration newConfig)</br>
    通过newConfig.getLayoutDirection()获取布局方向:</br>
    <pre>public int getLayoutDirection() {
    return (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == SCREENLAYOUT_LAYOUTDIR_RTL
    ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
    }</pre>
  • 强制设置配置</br>
    <pre>private void resetLocal() {
    Configuration config = getResources().getConfiguration();
    Locale locale = App.appLocale();
    Locale.setDefault(locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    config.setLocale(locale);
    config.setLayoutDirection(locale);
    } else {
    config.locale = locale;
    }
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());}</pre>
    getResources().updateConfiguration()方法在17版本以后过时,替换为下面的方法
    <pre>ContextWrapper public Context createConfigurationContext (Configuration overrideConfiguration
    </pre>

关于时区##

  • 获取当地时间 Locale.CHINA表示使用地区文字样式,setTimeZone获取哪个时区的时间
    <pre>public static String timeFormat(long time, String locale) {
    DateFormat dateFormat1 = new SimpleDateFormat("HH:mm dd-MM-yyyy", Locale.CHINA);
    dateFormat1.setTimeZone(TimeZone.getTimeZone("GMT+3"));
    //dateFormat.setTimeZone(TimeZone.getTimeZone(locale));
    return dateFormat1.format(time);
    }
    </pre>

关于字符串的格式化##

  • 字符串的格式化</br>
    可以使用有两个参数的format方法
    <pre>public static String format(Locale l, String format, Object... args) {
    return new Formatter(l).format(format, args).toString();
    }
    </pre>
  • TextView中设置显示的语言 防止例如在阿拉伯语环境下,数字会以阿拉伯语的方式展示
    <pre>if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    textView.setTextLocale(Locale.ENGLISH);
    }
    </pre>

相关文章

网友评论

      本文标题:Android 国际化笔记

      本文链接:https://www.haomeiwen.com/subject/kowdzttx.html