【问题标题】:Changing the locale is modifying the text dimensions of my app更改语言环境正在修改我的应用程序的文本尺寸
【发布时间】:2014-07-23 18:44:44
【问题描述】:

我正在尝试更改我的 Android 应用程序的语言,我正在使用此代码:

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 文件放在其中。

更改语言正常工作,但问题是它改变了我的应用程序中的文本尺寸(文本视图、菜单、对话框、编辑文本、按钮上的文本大小......基本上在我所有的应用程序中)。

删除更改区域设置的块代码,应用程序布局恢复正常。 getDisplayMetrics() 是否造成了问题?

我正在为一些文本使用样式:

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

还有一些文本视图的例子:

<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"/>

您能告诉我为什么会发生这种情况以及如何解决吗? 如果您需要任何其他信息,请询问。

【问题讨论】:

    标签: java android locale


    【解决方案1】:

    经过几个小时的搜索,我在这里的评论中找到了解决方案:Android changing language configuration messes up layout

    基本上你必须更换

    Configuration config = new Configuration();
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 2011-03-24
      • 2017-05-09
      • 1970-01-01
      • 2014-05-26
      • 2011-09-04
      • 2020-03-20
      • 2013-04-01
      相关资源
      最近更新 更多