Android语言切换
来源:互联网 发布:淘宝首页改版 编辑:程序博客网 时间:2024/05/20 14:24
首先在项目的res目录下新建Android XML File,Resource Type选择为Values,File的名字命名要统一这里我命名为strings.xml(一般默认新建的项目都是这个)
之后next,在左边选择Language,点击后选择语言。
之后就是代码了首先是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lang.core" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" /> <application android:allowBackup="true" android:configChanges="locale" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lang.core.ChangeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
主要是这句声明 android:configChanges="locale"
接着是ChangeActivity
package com.lang.core;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.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ChangeActivity extends Activity {private Button btn1, btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_change);btn1 = (Button) findViewById(R.id.button1);btn2 = (Button) findViewById(R.id.button2);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switchLanguage(Locale.CHINA);refresh();}});btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switchLanguage(Locale.ENGLISH);refresh();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_change, menu);return true;}private void refresh() {finish();Intent myIntent = new Intent(ChangeActivity.this, ChangeActivity.class);startActivity(myIntent);}public void switchLanguage(Locale locale) {Resources resources = getBaseContext().getResources();Locale.setDefault(locale);Configuration config = resources.getConfiguration();DisplayMetrics dm = resources.getDisplayMetrics();config.locale = locale;resources.updateConfiguration(config, dm);}}主要在于switchLanguage这个方法,设置语言后要重启下Activity,这样才能生效。
下载地址 点击打开链接
0 0
- android语言切换问题
- android的语言切换
- android的语言切换
- Android语言切换处理
- Android 语言切换
- Android 语言切换要点
- android语言切换
- Android语言切换
- android语言快速切换
- android语言切换
- Android语言切换Demo
- android多语言切换
- Android 语言切换
- Android 启动语言切换
- android多语言切换
- android 语言切换分析
- Android多语言切换
- Android多语言切换
- Inside The c++ Object Model 读书笔记
- 我们需要根据设备的寄存器编写驱动么?
- iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- css控制div显示/隐藏方法及2种方法比较原码
- 分享溜客2011年5月16日被检测过程
- Android语言切换
- 多进程通信(IPC)--共享内存
- strcspn和strspn函数
- oracle tool
- 数控机床典型系统模块化维修丛书:数控机床SINUMERIK系统模块化维修
- JPA @PersistenceContext及@Transactional Annotation
- 知道吗?你就是这样把我推远的。
- include .h 以及.cpp的记录
- 谜题5:十六进制的趣事