"Android Basics" "Internationalization and language selection"

Source: Internet
Author: User

Reference Source: http://download.csdn.net/detail/barryhappy/7176709

The project using SUPPORT.V7 has been modified to become an activity that does not use actionbaractivity. Reduce the error.

http://download.csdn.net/detail/mcdullsin/8290923

First, when speaking multiple languages, say how to reduce the "hard code"

What is "hard code", such as the use of a Chinese character string in a Java file. The right thing to do is to put it in a resource file and reference it where needed.

Say how to use resources in String.xml in places such as Java files:

There are several different places to get the values in the String.xml file.

1. In XML files such as Androidmanifest.xml and layout:

android:text= "@string/resource_name"

2. In the activity:

Method One: This.getstring (r.string.resource_name);

Method Two: Getresources (). getString (R.string.resource_name);

3. In other Java files (must have context or pplication)

Method One: Context.getstring (r.string.resource_name);

Method Two: Application.getstring (r.string.resource_name);

Second, the next analysis of multi-language source code and how to use

First effect:

This Big Brother's blog is a good illustration of the internationalization of Android multilingual:

Http://www.cnblogs.com/bluestorm/archive/2013/04/01/2993554.html

The multi-lingual principle of Android is this:

It will check the default configuration of the Android phone and then go to the specific values below to read the string.

The eldest Brother blog post words:

In the Res directory of the Android project, multiple language support can be achieved by defining special folder names. For example, our program is compatible with Simplified Chinese, English, set the default Strings.xml in the Values folder, and then build the Values-zh-rcn folder.

placing a strings.xml,strings.xml in each folder is a string of languages. If the parameter configuration class XML folder name is involved, also change to Xml-zh, XML. This allows for language switching in Android systems, and the programs you develop will then switch languages.

A special folder name!

The description folder name is fixed for each.

So analyze what's going on in this blog post.

What it does click menu will pop up the language settings and then switch to the language.

Java source code is as follows:

 PackageBarry.demo.multilanguage;ImportJava.util.Locale;Importandroid.support.v4.app.Fragment;Importandroid.app.Activity;Importandroid.content.res.Configuration;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;ImportAndroid.os.Build; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {Configuration config=getresources (). GetConfiguration (); Switch(Item.getitemid ()) { CaseR.id.action_english:config.locale=Locale.english;  Break;  CaseR.id.action_simple_chinses:config.locale=Locale.simplified_chinese;  Break;  CaseR.id.action_traditional_chinese:config.locale=Locale.traditional_chinese;  Break; default:            return true;        } getresources (). updateconfiguration (config, getresources (). Getdisplaymetrics ());        ((TextView) Findviewbyid (R.id.textviewhello)). SetText (R.string.hello_world); return true; }}

The main layout file is as follows:

 <framelayout xmlns:android= "http://schemas.android.com/apk/res/android"  Xmlns:tools  = "Http://schemas.android.com/tools"  Android:id  = "@+id/ Container " Android:layout_width  =" Match_parent "  Android:layout_height  =" Match_parent " tools: Context  = "barry.demo.multilanguage.MainActivity"  Tools:ignore  = " Mergerootframe "> <textview android:id  =" @+id/textviewhello " Android:layout_width  = "Match_parent"  Android:layout_height  =" 218 DP " android:gravity  =" center " Android:text  = "@string/hello_world"/></FRAMELAYOUT> 

The menu layout file is as follows:

<menu 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"Tools:context= "Barry.demo.multilanguage.MainActivity" > <Item Android:id= "@+id/action_english"android:orderincategory= "100"Android:title= "@string/english"/> <Item Android:id= "@+id/action_simple_chinses"android:orderincategory= "100"Android:title= "@string/simple_chinese"/> <Item Android:id= "@+id/action_traditional_chinese"android:orderincategory= "100"Android:title= "@string/traditional_chinese"/></menu>

One of the key issues here is:

When the button is clicked, the file is refreshed, each string TextView, and the ListView is refreshed.

Getresources (). updateconfiguration (config, getresources (). Getdisplaymetrics ());((TextView) Findviewbyid ( R.id.textviewhello). SetText (R.string.hello_world);;

If the server is involved, it's not refreshing.

And even if the local file refresh amount is a bit large.

So instead of having a language toggle button on every page, you should place the language toggle button on the landing page.

"Android Basics" "Internationalization and language selection"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.