Android应用的多语言切换
来源:互联网 发布:网络推广微信营销方案 编辑:程序博客网 时间:2024/06/05 23:48
在一些应用需要国际化或者面对各种用户群体时,会涉及到语言的不同,所以我们在开发应用apk时,记得一定要养成将应用中涉及到的所有文字信息放在string.xml里的好习惯!Android中的多语言切换,对于我们应用层的开发同学来时,操作起来非常见到,代码也很简单,大部分工作都是让framwork层的资源进程调度框架给代工了。OK,下面我们一步步看看怎么完成多语言的配置和切换:
1.右击res目录,打开对应的对话框:
2.选择local选项,然后点击右推的按钮,创建和选择新的语言资源文件,注意以下这几个地方需要正确填写:
然后点击ok就会开发项目目录中多了一个文件夹和文件:
3.接着把默认的values/strings.xml中的内容全部复制到values-en/strings.xml中,然后把内容中的文字顺便翻译成对应的语言:
4.接下来就是在activity中引用和设置的操作了:
首先需要定义一个全局变量存放activity栈和选择的语言类别:
public class Constants { //系统默认是zh的类型 public static String langae = "zh"; public static List<Activity> activityList = new ArrayList<>();}
MainActivity.java中:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Constants.activityList.add(this); switchLanguage(Constants.langae); } public void onclick(View view) { startActivity(new Intent(this, SecendActivity.class)); } //核心设置的代码 protected void switchLanguage(String language) { Resources resources = getResources(); Configuration config = resources.getConfiguration(); DisplayMetrics dm = resources.getDisplayMetrics(); switch (language) { case "zh": config.locale = Locale.CHINESE; resources.updateConfiguration(config, dm); break; case "en": config.locale = Locale.ENGLISH; resources.updateConfiguration(config, dm); break; default: config.locale = Locale.US; resources.updateConfiguration(config, dm); break; } }}
在onCreate()中设置语言的默认值。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@color/colorPrimary" android:onClick="onclick" android:padding="10dp" android:text="@string/lanage" android:textColor="#fff" /></RelativeLayout>
SecendActivity.xml:
public class SecendActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.atty_secend_layout); Constants.activityList.add(this); RadioGroup radio = (RadioGroup) findViewById(R.id.radio); final RadioButton radioButtonen = (RadioButton) findViewById(R.id.radio_en); final RadioButton radioButtonzh = (RadioButton) findViewById(R.id.radio_zh); if (Constants.langae.equals("zh")) { radioButtonzh.setChecked(true); } else { radioButtonen.setChecked(true); } radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.radio_zh) { Constants.langae = "zh"; radioButtonen.setChecked(false); } else { radioButtonzh.setChecked(false); Constants.langae = "en"; } } }); } public void onClick(View view) { Locale locale = new Locale(Constants.langae); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; Resources resources = getResources(); resources.updateConfiguration(config, resources.getDisplayMetrics()); //让之前打开的所有界面全部彻底关闭 for (Activity activity : Constants.activityList) { activity.finish(); } //回到应用的首页 startActivity(new Intent(this, MainActivity.class)); }}
atty_secend_layout.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:orientation="horizontal"> <RadioButton android:id="@+id/radio_zh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/zh" /> <RadioButton android:id="@+id/radio_en" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="@string/en" /> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@color/colorPrimary" android:onClick="onClick" android:padding="10dp" android:text="@string/click" android:textColor="#fff" /></RelativeLayout>
5.写完代码后,我们运行看看:
设置前:
设置后:
阅读全文
1 0
- Android应用的多语言切换
- Android多语言,应用内切换语言
- 关于Android应用内多语言切换的问题
- Android应用实现多语言切换
- Android 应用内切换语言
- android 应用内切换语言
- android 应用内切换语言
- android 应用内语言切换
- android 应用内切换语言
- Android应用内部切换语言
- [Android]应用语言切换 国际化
- android 应用内切换语言
- 新应用的知识整理-Android studio中应用多语言支持及应用内语言切换的实现
- 新应用的知识整理-Android studio中应用多语言支持及应用内语言切换的实现
- android实现只切换本应用的语言(不切换android 系统语言)
- Android基础--Android app应用多语言切换功能实现
- Android应用内切换应用语言
- [Android]应用语言切换的三种方法
- 解决列表闪烁问题
- python进阶知识点
- 访问对象
- poj 字符串相关的水题
- Linux移动物体监控程序Motion的移植
- Android应用的多语言切换
- [python网站开发] 一.Django入门知识及创建第一个网站
- 【算法期末作业】8.3 证明STINGY SAT是NP-完全问题
- poj 2752_Seek the Name, Seek the Fame_KMP
- Android Studio类中实现Serializable自动生成serialVersionUID
- Android-N app seinfo设置流程
- 表驱动初步接触之Map代替if/switch
- 比特币交易(Transaction)的输入与输出
- 面试题10—二进制中1的个数