zoukankan      html  css  js  c++  java
  • Android 国际化

    国际化:根据系统的语言环境,选择对应的文本显示。

    例:系统使用简体中文,就显示“你好”,系统使用美式英语,就显示“hello”。

    1、新建一个strings.xml文件

    对项目根目录/res目录单击右键 -> New -> Android Resource File   

    或 对values目录单击右键 -> New -> Value resource file

    在生成的xml文件中编写中文版字符串:

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <string name="app_name">测试</string>
    4     <string name="hello">你好</string>
    5 </resources>

    2、以同样的方式再新建一个strings.xml文件:

    在xml文件中编写英文版字符串,要与中文版一一对应(name要对应):

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <string name="app_name">test</string>
    4     <string name="hello">hello</string>
    5 </resources>

    AS会自动建立一个strings(和文件名相同)文件夹来保存同名的文件。

    3、可以看到<string>标签的name都有红色下划线,这是因为我们未设置默认值,即当用户的语言环境既不是简体中文,又不是美式英语时,就显示默认文本。

    鼠标单击红色下划线部分,Alt+Enter查看解决方案,依次点击:

    双击聚焦输入默认值,完成后刷新一下即可:

    4、在布局的xml文件、.java文件中不要直接用字符串,而是使用@strings/xxx的方式引用我们在strings.xml文件中定义的值。

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"/>

    debug,看到虚拟机显示的是英文版文本,这是因为虚拟机的默认语言环境是美式英语。

    Settings -> Languages input -> lanuages -> 将第一个语言设置为简体中文

    再次debug,虚拟机显示的即是中文版的文本。

  • 相关阅读:
    unit3d 4.6 document open solution
    Unity3dBug
    linq to xml
    A const field of a reference type other than string can only be initialized with null Error [duplicate]
    Redis数据类型
    redis快照与AOF
    redis实现高并发下的抢购/秒杀功能
    xss攻击怎么防止
    四种常见的索引类型
    什么是sql 注入及如何预防 sql 注入
  • 原文地址:https://www.cnblogs.com/chy18883701161/p/10873158.html
Copyright © 2011-2022 走看看