更改语言环境正在修改我的应用程序的文本尺寸 [英] Changing the locale is modifying the text dimensions of my app

查看:64
本文介绍了更改语言环境正在修改我的应用程序的文本尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用上更改语言,并且正在使用以下代码:

I'm trying to change the language on my Android app and I'm using this code:

String languageToLoad  = "en";
Locale locale = new Locale(languageToLoad); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, 
getBaseContext().getResources().getDisplayMetrics());

我创建了不同的values目录(\values-fr\values-it等),在其中放置了我的string.xml文件.

I've created different values directories (\values-fr, \values-it, etc.) where I put my string.xml files.

更改语言可以正常进行,但问题在于它正在更改我的应用程序中的文本尺寸(基本上在我所有的应用程序中,textviews,菜单,对话框,edittexts,button的文本大小).

Changing the language works normally, but the problem is that it's changing text dimensions in my app (text sizes on textviews, menu, dialogs, edittexts, buttons...basically in all my app).

删除用于更改语言环境的阻止代码,应用布局恢复正常. getDisplayMetrics()是造成问题吗?

Removing the block code that changes the locale, the app layout gets back to normal. Is the getDisplayMetrics() creating the problem?

我在某些文本中使用样式:

I'm using styles for some of the texts:

<style name="title_font">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#095080</item>
        <item name="android:textSize">26sp</item>
        <item name="android:textStyle">bold</item>
</style> 

还有一些文本视图示例:

And some example of textviews:

<TextView
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        style="@style/title_font"
        android:text="@string/login_title" />

<TextView
      android:id="@+id/label_user"
      android:textStyle="bold"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginTop="50dp"
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="@string/user_label"
      android:layout_below="@+id/login"/>

您能告诉我为什么会这样以及如何解决吗? 如果您需要任何其他信息,请询问.

Can you tell me why is this happening and how to solve it? If you need any additional information, please ask.

推荐答案

经过几个小时的搜索,我在以下评论中找到了解决方案:

After a few hours of searching, I found the solution in a comment here: Android changing language configuration messes up layout

基本上您必须更换

Configuration config = new Configuration();

使用

Configuration config = getBaseContext().getResources().getConfiguration();

这样,您无需创建新配置,而只需修改现有配置即可.

This way, you don't create a new configuration, but only modifying the existing one.

这篇关于更改语言环境正在修改我的应用程序的文本尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆