目录
编写语言管理类
编写Activity 的父类
DEMO 实验界面--首页Activity
DEMO 实验界面--设置语言Activity
Demo 语言资源文件
参考连接
编写语言管理类
package com.example.languageapplication
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.util.Log
import java.util.Locale
class LanguageContextWrapper {
companion object {
const val TAG = "LanguageContextWrapper"
//对应方式一
var curAppLanguageNew:String? = null;
//对应方式二
var curAppLanguageLocaleNew:Locale? = null;
/**
* 方式一:设置字符串的方式
* */
fun wrapString(context: Context): Context {
if(curAppLanguageNew == null){
return context
}
val config: Configuration = context.resources.configuration
val localeItem = Locale(curAppLanguageNew)
val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(localeItem)
val localeList = LocaleList(localeItem)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
context.createConfigurationContext(config)
} else {
config.setLocale(localeItem)
context.createConfigurationContext(config)
}
return ContextWrapper(mContext)
}
/**
* 方式二:设置Locale的方式
* */
fun wrapLocale(context: Context): Context {
if(curAppLanguageLocaleNew == null){
return context
}
val config: Configuration = context.resources.configuration
val mContext = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(curAppLanguageLocaleNew)
val localeList = LocaleList(curAppLanguageLocaleNew)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
context.createConfigurationContext(config)
} else {
config.setLocale(curAppLanguageLocaleNew)
context.createConfigurationContext(config)
}
return ContextWrapper(mContext)
}
fun getCurLanguage(context: Context): String {
var languageCode = "";
var curLocale: Locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.resources;
curLocale = context.resources.configuration.locales.get(0);
} else {
curLocale = context.resources.configuration.locale;
}
if (curLocale != null) {
languageCode = curLocale.language;
}
Log.d(TAG, "当前语言:$languageCode")
return languageCode;
}
}
}
下面用的是方式一
编写Activity 的父类
package com.example.languageapplication
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle
open class BaseActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
}
override fun attachBaseContext(newBase: Context) {
if(LanguageContextWrapper.curAppLanguageNew != null){
super.attachBaseContext(LanguageContextWrapper.wrapString(newBase))
}else{
super.attachBaseContext(newBase);
}
}
}
DEMO 实验界面--首页Activity
package com.example.languageapplication
import android.content.Intent
import android.os.Bundle
import android.widget.Button
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button_two_activity).setOnClickListener{
val intent = Intent(this, TwoActivity::class.java)
startActivity(intent)
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button_two_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_start_text"
tools:layout_editor_absoluteX="38dp"
tools:layout_editor_absoluteY="91dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
DEMO 实验界面--设置语言Activity
package com.example.languageapplication
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import java.util.Locale
class TwoActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_two)
findViewById<Button>(R.id.button_switch).setOnClickListener {
if(LanguageContextWrapper.getCurLanguage(TwoActivity@this) == Locale.ENGLISH.language){
LanguageContextWrapper.curAppLanguageNew = Locale.CHINESE.language;
}else {
LanguageContextWrapper.curAppLanguageNew = Locale.ENGLISH.language;
}
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TwoActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_btn_text"
tools:layout_editor_absoluteX="34dp"
tools:layout_editor_absoluteY="37dp"
tools:ignore=",MissingConstraints" />
<Button
android:id="@+id/button_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/demo_switch_text"
tools:layout_editor_absoluteX="38dp"
tools:layout_editor_absoluteY="91dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Demo 语言资源文件
<resources>
<string name="app_name">LanguageApplication</string>
<string name="demo_switch_text">switch language</string>
<string name="demo_start_text">Start the second Activity</string>
<string name="demo_btn_text">english button</string>
</resources>
<resources>
<string name="app_name">语言APP</string>
<string name="demo_switch_text">切换语言</string>
<string name="demo_start_text">启动第二个Activity</string>
<string name="demo_btn_text">中文按钮</string>
</resources>
参考连接
locale - Android context.getResources.updateConfiguration() deprecated - Stack Overflow