推荐安卓开发神器(里面有各种UI特效和android代码库实例)

每一个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"/>



更多相关文章

  1. Android 中文 API (24) ―― MultiAutoCompleteTextView.CommaToke
  2. Android应用开发提高系列(5)——Android动态加载(下)——加载已安装A
  3. Android 中文api (88)――SharedPreferences
  4. Android API 中文 (42) —— ListView
  5. 福利!!!你想要的Android、Java、Python、QT等等视频资源都在这里
  6. Android中文资源站专栏:五个好用的日常软件
  7. Android中的资源分析
  8. android 查看apk中资源文件

随机推荐

  1. jquery常用代码片段
  2. JQuery的一些基本功能代码(CH1&2)
  3. 【jQuery】调用delay()方法延时执行动画
  4. 将JavaScript数组转换成逗号分隔列表的简
  5. 想要改进我的jQuery代码,做我想要的,太丑了
  6. Struts2使用jQuery JSONP解决跨域问题
  7. Ajax/jQuery -在页面加载时将网页内容加
  8. jQuery弹出窗口不能打开。
  9. 将php jsonencode数组结果显示为ajax成功
  10. 【JavaScript】jQuery+ajax传递json数组(