Android本地化问题: Not all items in the layout update properly when switching locales

标签 android layout localization

这就是问题所在:当我有一个 Activity 在后台运行时,我切换区域设置,然后切换回应用程序,所有内容都会更新...除了具有“android:id”属性集的复选框和单选按钮.

如果复选框和单选按钮没有“android:id”属性,那么它们会更新 OK。其他字段没有这个问题,不管它们是否有“android:id”属性。

什么是确保在区域设置更改时更新我正在运行的 Activity 中的所有内容的最佳方法?

重现步骤:

1) 在 Eclipse 中创建一个“Hello, Android”项目。 2) 在主布局中,定义两个复选框:

<CheckBox android:text="@string/checkbox" android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="@string/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>

3) 创建两个strings.xml:一个在“values”下,一个在“values-es”下。

4) 在“值”下创建以下字符串:

<string name="checkbox">English</string>

5) 在“values-es”下创建如下字符串

<string name="checkbox">español</string>

6) 将设备设置为“英语”

7) 在模拟器或任何设备上运行应用程序(在 HTC G1 上测试)。

8) 观察。两个复选框都显示“英语”。

9) 按“主页”返回菜单并让应用程序在后台运行。

10) 转到设置。将语言切换为“español”

11) 按住“主页”。返回应用程序。

预期结果:

两个复选框都显示“español”

实际结果:

第一个复选框显示“英语”

第二个复选框显示“español”

带有“android:id”属性的复选框似乎没有更新。没有“android:id”属性的复选框按预期工作。

最佳答案

问题的原因是 CompoundButton.onSaveInstanceState() 调用了 setFreezesText(true) 并因此保存和恢复了文本。

一个简单的解决方案是使用这样的子类:

public class CheckBoxNoPersistentText extends CheckBox {

    public CheckBoxNoPersistentText(final Context context) {
        super(context);
    }

    public CheckBoxNoPersistentText(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckBoxNoPersistentText(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onRestoreInstanceState(final Parcelable state) {

        final CharSequence text = getText(); // the text has been resolved anew

        super.onRestoreInstanceState(state); // this restores the old text

        setText(text); // this overwrites the restored text with the newly resolved text

    }
}

关于Android本地化问题: Not all items in the layout update properly when switching locales,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4504024/

相关文章:

java - Android:如何使用按钮更改微调器的选择?

html - CSS !important 规则不覆盖文本对齐

ios - 如何在我的应用程序中添加泰米尔语。我的 Xcode 版本是 9.2

asp.net - 如何使用 app_GlobalResource 或 app_LocalResource?

ios - 在 ios 中处理自定义键盘类型

android - Retrofit 2 + RxJava 将类似的请求干净地链接在一起

android - Flex Mobile:无法在Android设备中播放AAC音频

android - ActionBar 中 Action /菜单项的动态控制

html - 调整浏览器窗口大小时如何防止表格和图像元素移动[REPOST]

android - 如何在选项卡的布局中添加布局