Android动态设置多语言
来源:互联网 发布:docker连接外部数据库 编辑:程序博客网 时间:2024/03/28 19:46
动态设置多语言
效果图:
需求:在设置中添加一个可以设置语言的需求,点击选中的语言,就可以切换显示。
demo代码:
APP类:app初始化的时候获取保存的语言(没有就系统默认),设置语言。
public class APP extends Application { private Object locale; @Override public void onCreate() { super.onCreate(); //设置语言 setLanguage(); } private void setLanguage() { Locale locale = getLocale(); Configuration configuration = getResources().getConfiguration(); DisplayMetrics metrics = getResources().getDisplayMetrics(); if (locale != null) { configuration.locale = locale; getResources().updateConfiguration(configuration, metrics); } else { configuration.locale = Locale.getDefault(); getResources().updateConfiguration(configuration, metrics); } } public Locale getLocale() { File file = new File(getFilesDir(), "locale"); if (!file.exists()) { return null; } ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(new FileInputStream(file)); return (Locale) objectInputStream.readObject(); } catch (Exception e) { } finally { try { if (objectInputStream != null) { objectInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; }}
SettingActivity页面:
public class SettingActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv_en; private TextView tv_zh; private TextView tv_ja; private TextView tv_ar; private TextView tv_defalut; private Configuration configuration; private DisplayMetrics displayMetrics; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); if (toolbar == null) return; toolbar.setTitle(getString(R.string.app_name)); toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); tv_defalut = (TextView) findViewById(R.id.defalut); tv_en = (TextView) findViewById(R.id.en); tv_zh = (TextView) findViewById(R.id.zh); tv_ja = (TextView) findViewById(R.id.ja); tv_ar = (TextView) findViewById(R.id.ar); configuration = getResources().getConfiguration(); displayMetrics = getResources().getDisplayMetrics(); tv_defalut.setOnClickListener(this); tv_en.setOnClickListener(this); tv_zh.setOnClickListener(this); tv_ja.setOnClickListener(this); tv_ar.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.defalut: configuration.locale = Locale.getDefault(); break; case R.id.en: configuration.locale = Locale.ENGLISH; break; case R.id.zh: configuration.locale = Locale.SIMPLIFIED_CHINESE; break; case R.id.ja: configuration.locale = Locale.JAPANESE; break; case R.id.ar: configuration.locale = new Locale("ar", ""); break; default: configuration.locale = Locale.getDefault(); break; } //切换语言时,同时改变水平布局的方向 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { configuration.setLayoutDirection(configuration.locale); } saveLocale(configuration.locale); getResources().updateConfiguration(configuration, displayMetrics); reloadApp(); } private void saveLocale(Locale locale) { if (locale == null) { locale = Locale.getDefault(); } File file = new File(getFilesDir(), "locale"); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); objectOutputStream.writeObject(locale); } catch (IOException e) { e.printStackTrace(); } finally { try { if (objectOutputStream != null) { objectOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void reloadApp() { Intent intent = new Intent(SettingActivity.this, LauncherActivity.class); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); /** * 设置singleTask启动模式 * 1.配置文件中设置launchMode="singleTask" * 2.页面跳转时setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) * * singleTask启动模式:当activity设置了该启动模式,再次跳转启动时,会将任务栈中的其他activity销毁,只让自己独自在栈中 */ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); /** * 设置singleTop启动模式 * 1.配置文件设置launchMode="singleTop"效果一样 * 2.页面跳转时setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) * * singleTop启动模式:当activity设置了该启动模式,再次跳转启动时,会将任务栈中,在该activity之上的activity销毁,保持该activityyi处于栈顶状态。 */ //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }}
strings.xml内容:
默认/en<string name="app_name">Multi_language</string><string name="title">Hello world</string>阿拉伯语/ar<string name="app_name">متعدد اللغات</string><string name="title">مرحبا العالم</string>日语/ja<string name="app_name">マルチ言語</string><string name="title">こんにちは世界</string>汉语/zh<string name="app_name">多语言</string><string name="title">你好,世界</string>
主要是通过activity的启动模式,重新启动app,做初始化操作,语言就设置成功了。
0 0
- Android动态设置多语言
- android动态设置app当前运行语言
- Android之动态设置App语言环境
- android多语言设置
- android多语言设置
- android多语言设置
- android 多语言设置
- Android 动态支持多语言
- android 动态设置全屏
- 动态全屏设置 android
- Android 动态设置margin
- Android 动态设置尺寸
- Android动态设置actionBar
- Android动态设置Shape
- Android 动态设置Shape
- android app 动态实现多语言切换
- android中多语言的设置
- android应用开发-----------------本地多语言设置
- clEnqueueNDRangeKernel()的各参数意义以及相关注意事项
- java的位运算符
- iOS每日一记————————简单的实现委托 Block代码块 功能 和简单的自定义View
- C# 实现像QQ一样隐藏窗体
- SpringBoot
- Android动态设置多语言
- Android之Eclipse下面如何看Android源代码
- 标准C++中的string类的用法总结
- Android ToolBar+Translucent System Bar的用法
- Global and Local Variables
- AIX密码丢失重置过程
- ssh连接超时问题解决方案
- Android开发技巧——设置系统状态栏颜色
- 使用docker搭建hadoop分布式集群