详解Android更改APP语言模式的实现过程
作者:daisy 发布时间:2023-05-18 16:56:35
标签:Android,app,多语言
一、效果图
二、描述
更改Android项目中的语言,这个作用于只用于此APP,不会作用于整个系统
三、解决方案
(一)布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hellow" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeLanguage"
android:text="语言切换" />
</LinearLayout>
(二)MainActivity主页面
package com.example.chinesepage;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 点击按钮,更换语言
*
* @param view
*/
public void changeLanguage(View view) {
Resources resources = getResources();
Configuration configuration = resources.getConfiguration(); // 获取资源配置
if (configuration.locale.equals(Locale.CHINA)) { // 判断当前语言是否是中文
configuration.locale = Locale.ENGLISH; // 设置当前语言配置为英文
} else {
configuration.locale = Locale.CHINA; // 设置当前语言配置为中文
}
DisplayMetrics metrics = new DisplayMetrics();
resources.updateConfiguration(configuration, metrics); // 更新配置文件
sendBroadcast(new Intent("language")); // 发送广播,广播接受后重新开启此Activtiy以重新初始化界面语言.
// Intent intent = new Intent(MainActivity.this, MainActivity.class); //或者可以直接跳转MainActivity
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); //去除掉跳转的动画,让用户看起来好像没有跳转的感觉
// startActivity(intent);
finish();
}
}
(三)ChangeReceiver广播类
package com.example.chinesepage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 自定义广播类 语言改变后重启Activity
*
* @author asus
*
*/
public class ChangeReceiver extends BroadcastReceiver {
private Intent mIntent;
@Override
public void onReceive(Context context, Intent intent) {
mIntent = new Intent(context, MainActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
(四)在Res下创建Values-en文件夹,复制String.xml,并且把里面的中文改成英文,实现国际化.
values/strings.xml
<resources>
<string name="app_name">语言切换</string>
<string name="hello_world">你好,World!</string>
<string name="action_settings">设置</string>
<string name="hellow">你好</string>
</resources>
values-en/strings.xml
<resources>
<string name="app_name">ChinesePage</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="hellow">Hellow</string>
</resources>
(五)注册广播(这个别忘了~)
<receiver android:name="com.example.chinesepage.ChangeReceiver" >
<intent-filter>
<action android:name="language" />
</intent-filter>
</receiver>
总结
0
投稿
猜你喜欢
- 前言最近项目需要和Oracle数据库进行交互,然后我从Maven中央仓库下载数据库驱动jar包,但怎么都下不下来,我到Oracle官网上一看
- 本文实例讲述了Android编程自定义组件。分享给大家供大家参考,具体如下:在Android中,所有的UI界面都是由View类和ViewGr
- springboot连接sqllite的坑2021-01-04 13:54:14.178 SvUSService [main] ERROR
- 前言我们知道,Java项目编译后会生成许许多多的class文件,class文件保存着类的描述信息。虚拟机把描述类的数据从Class文件加载到
- 代码实例一 using System; using System.IO; using System.Collections; using S
- 一、概念从本质上来说,它就是一个匿名函数,可以用来直接实现接口中的方法,从而简化代码。但是Lambda有一个限制,不能实现接口中的所有方法,
- 一、准备工作mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的联表查询能力一直
- 一.Mybatis-Plus——sum聚合函数//总收益 Order order =new Orde
- //路径, 添加开机启动/删除开机启动 public s
- 背景产品想对多次快速点击做一下优化,想要的效果就是双击不会打开多次但是从开发角度来说,我可以用kotlin的拓展方法来调整这个,但是之前的历
- 本文研究的主要是JVM中的flag设置详解的相关内容,具体介绍如下。一、堆大小设置-Xmx3550m:设置JVM最大可用内存为3550M。-
- SmartArt其实就是一个文字的可视化工具,用户可在PowerPoint,Word,Excel中使用该特性创建各种图形图表。SmartAr
- 最近研究OpenCV想用java进行开发,因此研究了一下怎么在Eclipse中配置基于java的Opencv.第一步:先到OpenCV官网下
- 自己写的一个日历记事本效果图 具体步骤:1.添加控件SkinEngine。 1.右键“工具箱”。“添加选项卡”,取名“皮肤”。
- 前言最近测试给我提了一个bug,说我之前提供的一个批量复制商品的接口,产生了重复的商品数据。追查原因之后发现,这个事情没想象中简单,可以说一
- 在阅读这篇文章之前,大家可以先看下《Java多线程atomic包介绍及使用方法》,了解atomic包的相关内容。一、何谓Atomic?Ato
- 博主最近在做一个内网项目,内部可以访问外部数据,但是外部访问不了内部数据,这也就造成了可能文件无法上传,所以博主另辟蹊径,在本地服务器上建立
- 前言java有八个基本数据类型,每个都有对应的一个包装类,比如int对应的Integer。 Integer 是int的包装类型,数据类型是类
- 前言之前采取项目中嵌套html页面,实现基本的登录校验、权限校验、登出操作、记住我等功能试下。但是,现在的开发基本都是前后分离样式,后端并不
- 直接贴上代码,里面都有注释/// <summary> &n