2014-07-08 29 views
0

我知道android根据语言环境选择适当的值文件夹。但我想从代码中自己选择它。如何以编程方式引用所需的值文件夹?

<com.example.typefacetest.CustomTextView 
     android:id="@+id/banglaTextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     custom:banglaText="@string/hello_world" 
     android:text="@string/hello_world" 
     /> 

在我values-bn文件夹中,我有字符串文字命名hello_world其用于属性custom:banglaText。如果我没有设置语言环境,android将不会选择values-bn文件夹。现在我想以编程方式从该文件夹中提取字符串。如何做?

回答

0

您可以使用此:ResourceBundle或写是这样的:

Configuration configuration = getResources().getConfiguration(); 
configuration.locale = new Locale("en"); 
DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
Resources resources = new Resources(getAssets(), metrics, configuration); 
String string = resources.getString(id); 

在ID,你应该把字符串的ID从strings.xml中。

+0

我不想设置configuration.locale,因为这不被推荐。 http://stackoverflow.com/questions/2900023/change-language-programatically-in-android –

相关问题