0

我需要创建一个单例或静态类来更改应用程序的本地化,它将具有以下实现

public static void changeLocale( Context context , int layoutID ,int pos )
{
    if ( currentLocale == pos)
        return;
    Locale locale;
    if (pos == 0)
        locale = new Locale("en");
    else
        locale = new Locale("ar");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getApplicationContext().getResources().updateConfiguration(config,              context.getApplicationContext().getResources().getDisplayMetrics());
    //setContentView(layoutID);
}

它将从Activity或Dialog调用,我的问题是Context没有setContentView(),我应该传递什么接口或类而不是应该传递的上下文setContentView(),一种方法是传递一个对象并将其转换为ActivityDialog在 try catch 块中,但我相信有更好的方法可以做到这一点,谢谢你的帮助

4

1 回答 1

0

You don't need to set contentview when you change locale this is an excert from : http://developer.android.com/guide/topics/resources/localization.html

you only need to create a new layout for the specific locale called res/layout-localename/sameresourceinallotherfolders.xml

and it should change the content view accordingly, when you change the locale

Design a flexible layout

If you need to rearrange your layout to fit a certain language (for example German with its long words), you can create an alternative layout for that language (for example res/layout-de/main.xml). However, doing this can make your application harder to maintain. It is better to create a single layout that is more flexible.

Another typical situation is a language that requires something different in its layout. For example, you might have a contact form that should include two name fields when the application runs in Japanese, but three name fields when the application runs in some other language. You could handle this in either of two ways:

Create one layout with a field that you can programmatically enable or disable, based on the language, or Have the main layout include another layout that includes the changeable field. The second layout can have different configurations for different languages. Avoid creating more resource files and text strings than you need

You probably do not need to create a locale-specific alternative for every resource in your application. For example, the layout defined in the res/layout/main.xml file might work in any locale, in which case there would be no need to create any alternative layout files.

Also, you might not need to create alternative text for every string. For example, assume the following:

Your application's default language is American English. Every string that the application uses is defined, using American English spellings, in res/values/strings.xml. For a few important phrases, you want to provide British English spelling. You want these alternative strings to be used when your application runs on a device in the United Kingdom. To do this, you could create a small file called res/values-en-rGB/strings.xml that includes only the strings that should be different when the application runs in the U.K. For all the rest of the strings, the application will fall back to the defaults and use what is defined in res/values/strings.xml.

Use the Android Context object for manual locale lookup

You can look up the locale using the Context object that Android makes available:

String locale = context.getResources().getConfiguration().locale.getDisplayName();

于 2013-10-07T07:23:41.223 回答