阅读更多

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

每一个Activity中都要加: android:configChanges="locale"。

是为了解决如下问题:

中文资源:

<?xml version="1.0" encoding="utf-8"?>

你好,这是测试文字!
LanguageTest
Change to English

英文资源:

<?xml version="1.0" encoding="utf-8"?>


Hello World, MainActivity!
LanguageTest
变成中文

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中:




android:label="@string/app_name"

android:configChanges="locale">
















android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:anyDensity="true"/>



更多相关文章

  1. Android(安卓)Resource介绍和使用
  2. android中文api(89)——ViewManager
  3. Android(安卓)中文API(86)——ResourceCursorAdapter
  4. Android(安卓)组件资源库
  5. Android(安卓)Studio & ADT 快捷键配置文件所在目录,自定义后可导
  6. Android中文API(144) —— JsonWriter
  7. android 中文api (62) —— ViewSwitcher.ViewFactory
  8. android api 中文 (74)—— AdapterView.AdapterContextMenuInfo
  9. Android(安卓)中文 API (27) —— SeekBar.OnSeekBarChangeListene

随机推荐

  1. android 网络连接保活
  2. Android引导页Splash设计
  3. Android(安卓)6.0新控件属性记录
  4. Android初始篇二 Android四大组件之Activ
  5. Android中悬浮窗口的实现原理和示例代码
  6. android kotlin 学习笔记基础篇(一)
  7. android使用Camera调用系统原生的摄像头(
  8. android的应用包名与代码包名
  9. Android(安卓)中activity实现全屏无标题
  10. 【Android】JNICALL could not be resolv