3

我的应用程序有两种语言。从右到左和从左到右。我在应用程序内部更改语言环境(不是从电话设置)。并在 resume() 函数上重新加载数据以查看 UI 上的更改。我有两个值文件夹。一个值-en,一个值-fa。要将 textview 的方向更改为 RTL 和 LTR,请将两个 styles.xml 放在这些文件夹中。样式.xml(值-en):

<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Direction">
    <item name="android:gravity">left</item>
    <item name="android:layout_gravity">left</item>
</style>
</resources>

样式.xml(值-fa):

<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Direction">
    <item name="android:gravity">right</item>
    <item name="android:layout_gravity">right</item>
</style>
</resources>

并使用这种风格,例如我这样做:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/menutext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/Direction"
android:padding="0dp"/>
</RelativeLayout>

如果我运行应用程序,一切正常,并且 textview 的方向与语言环境有关。问题就在这里,当我从选项菜单中更改应用程序设置的语言时,数据加载正确,但方向不正确。我应该去另一个活动以查看方向的变化。

为了解决这个问题,我可以删除 styles.xml,因为我正在使用 Html.fromHtml() 并且我的数据有

<p dir="LTR">

<p dir="RTL">

标签?

另一个可能的解决方案(感谢 TOMCA 的评论):android 在运行时动态更改样式

但问题仍然存在。有人知道吗?

4

1 回答 1

2

做这里解释的事情: android dynamic change style at runtime

对于恢复问题:只需尝试在 onResume 中调用 setContentView(),如下所示:

@Override
 protected void onResume() {
     if(selectedLang.equalsIgnoreCase("fa"))
    context.setTheme(R.style.CustomTheme_DirectionRight);
     else
    context.setTheme(R.style.CustomTheme_DirectionLeft);
     super.onResume();
     setContentView(R.layout.content);
     findAgainViews();//because recalling setContentView reset all findViewById()
 }
于 2012-10-06T09:14:54.847 回答