Android控制自身应用设置实现多语言
来源:互联网 发布:淘宝店宝贝描述 编辑:程序博客网 时间:2024/04/20 08:10
每一个Activity中都要加: android:configChanges="locale"。
加 <supports-screens>是为了解决如下问题:
中文资源:
< xml version="1.0" encoding="utf-8" >
<resources>
<string name="hello">你好,这是测试文字!</string>
<string name="app_name">LanguageTest</string>
<string name="btn_name">Change to English</string>
</resources>
英文资源:
< xml version="1.0" encoding="utf-8" >
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">LanguageTest</string>
<string name="btn_name">变成中文</string>
</resources>
package com.hkp;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
public String [] langes = {"zh","en"};
public static int count = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.hello1);
String str = this.getResources().getString(R.string.hello);
tv.setText(str);
Button btn = (Button)findViewById(R.id.btn_change);
btn.setText(R.string.btn_name);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String languageToLoad = langes[count++%2];
setLanguage(languageToLoad);
//刷新界面
Intent intent = new Intent();
intent.setClass(MainActivity.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(intent);
}
});
}
/**
* Set the Language of Application
*
* @param lang
* the language to set
*/
private void setLanguage(String lang) {
String languageToLoad = lang;
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
}
}
AndroidManifest.xml中:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
- Android控制自身应用设置实现多语言
- Android 应用语言设置的实现
- android 应用重新启动自身
- Android研究之监听自身应用被卸载代码实现
- android应用开发-----------------本地多语言设置
- 关于Android应用多语言支持实现
- Android应用实现多语言切换
- android监听应用自身被卸载
- Android获取应用自身相关签名信息
- android 如何监听自身应用被卸载
- android重启自身应用APP
- android监听应用自身被卸载
- Android对应用自身评分+app分享
- Android 关闭非自身应用的界面
- Android获取应用自身相关签名信息
- Android应用结束自身进程的方法
- android 如何监听自身应用被卸载
- Android简单语音控制应用的实现
- 三层交换机连接路由器
- 自定义UIAlertView可以容纳多个按钮
- 架构师---名称介绍
- C/C++指针常量和常量指针
- Comet HTTP服务器推送框架之icomet
- Android控制自身应用设置实现多语言
- BZOJ 1191 超级英雄hero(二分图匹配,匈牙利算法)
- MySQL字符编码:如何从utf8升级到utf8mb4
- 关于远程调用实现的必要技术
- Java 加解密技术系列之 MD5
- Servlet3.0 服务端推技术实例
- android handler理解+使用方法
- iOS学习笔记(七)——UI基础UIButton
- Linux下停止tomcat